Test the controller created by Spring MVC from JUnit using MockMVC.
You can reproduce the behavior of Spring MVC without deploying to the application server. MockMVC makes it easy to create controller tests. To use MockMVC with Spring Boot, add the following description to build.gradle.
build.gradle
testCompile('org.springframework.boot:spring-boot-starter-test')
When you create a project with SPRING INITIALIZR https://start.spring.io/, it is described in build.gradle by default.
In order to try MockMVC this time, I created a project with SPRING INITIALIZR https://start.spring.io/. The project settings are as follows. Since Spring MVC and Thymeleaf are used, please select as you like except to add Web and Thymeleaf to dependency.
--Spring Boot version: 2.0M7 --Build tool: Gradle --Language: Kotlin
In this environment, create a page with two text boxes and a registration button for entering the user name and email address as shown below.
Test with MockMVC that the input page is displayed when accessing the root URL from the browser. Since the purpose is to try MockMVC, processing other than page display is omitted.
Create a controller that displays index.html for GET requests to the root URL.
UserController.kt
@Controller
class UserController {
@RequestMapping(value = "/", method = [(RequestMethod.GET)])
fun index(model: Model): String {
model.addAttribute("userForm", UserForm())
return "index"
}
}
Create a class for the input form. Kotlin allows you to use the data class, so it's simple to write.
UserForm.kt
data class UserForm(val name: String = "", val mail: String = "")
I added the following to build.gradle to use BootStrap.
build.gradle
compile group: 'org.webjars', name: 'bootstrap', version: '3.3.7-1'
In Thymeleaf, create a page with a textbox and a button as shown below.
index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>user registration</title>
</head>
<body>
<div class="container">
<h1>user registration</h1>
<form id="userForm" method="post" th:action="@{/user}" th:object="${userForm}" action="/user">
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" id="name" type="text" th:feild="*{name}" name="name" placeholder="Name"/>
</div>
<div class="form-group">
<label for="mail">Mail</label>
<input class="form-control" id="mail" type="text" th:feild="*{mail}" name="mail" placeholder="Mail"/>
</div>
<button type="submit" class="btn btn-primary">Registration</button>
</form>
</div>
</body>
</html>
Send a GET request to the root URL and test the following in the response obtained. --Index.html is displayed as a view --Status code is 200 --The values of the name attribute, placeholder attribute, and value attribute of the text box are correct.
Create a test class as follows.
UserControllerTest.kt
@RunWith(SpringRunner::class) // ①
@WebMvcTest // ②
class UserControllerTest {
@Autowired
private lateinit var mockMvc: MockMvc // ③
@Test
fun test1() {
this.mockMvc.perform(get("/")) // ④
.andExpect(status().isOk) // ⑤
.andExpect(view().name("index")) // ⑥
.andExpect(xpath("/html/head/title").string("Title")) // ⑦
.andExpect(xpath("""//*[@id="name"]/@name""").string("name"))
.andExpect(xpath("""//*[@id="name"]/@placeholder""").string("Name"))
.andExpect(xpath("""//*[@id="name"]/@value""").string(""))
.andExpect(xpath("""//*[@id="mail"]/@name""").string("mail"))
.andExpect(xpath("""//*[@id="mail"]/@placeholder""").string("Mail"))
.andExpect(xpath("""//*[@id="mail"]/@value""").string(""))
}
}
―― ① Used to test Spring with JUnit. --② Use @WebMvcTest to test views and controllers using MockMVC. --③ DI the instance of MockMvc --④ Send a GET request ――⑤ Check the HTTP status. --⑥ Check that index.html is displayed as a view. --⑦ Check that each attribute of the text box is correct. You can get the attribute value of the textbox by XPath
You can run it as a JUnit test from the IDE. When specifying the test class from gradlew, specify as follows.
./gradlew test --tests "package.ControllerTest"
Recommended Posts