Daily verbose
Accept the previous article Integration and optimization of Spring+Spring MVC+MyBatis+easyUI (4) Unit test examples The unit testing of dao layer and service layer has been explained, and the controller layer can not be omitted. So this article will talk about the unit testing of MockMvc and controller layer. The relevant code has been uploaded and can be downloaded by itself.
My github address
Why use MockMvc?
Maybe the code in the test control layer is to start the server, enter the URL in the browser, and then start testing whether it achieves the desired effect. If there are errors, modify the relevant code and restart the server to test again. To analyze this process, start the server --> open the browser --> enter the URL --> wait for the return result --> fix the bug --> restart the server --. Cycle.
The disadvantage is also obvious. In the browser, it is better to enter the address of the URL. If it is a GET request, what about a POST request or a DELETE request? You can only use other tools to write curl statements on the command line, or use the postman plug-in of Google Browser, or write the corresponding httpClient method in your code, but these methods are more troublesome, and test cases can not be saved well. Another disadvantage is that after code modification, it is often necessary to restart the server again, waiting for the start-up to complete the next test process.
If the tomcat server starts slowly, it will be a very painful thing, test verification is inconvenient, and depends on the network environment, these reasons lead to testing is very troublesome, and in order to facilitate the testing of ontroller, and to save and recycle test cases well, you can solve it through unit testing, through the previous article, you for The convenience of unit testing has been recognized and realized, and then through the introduction of MockMVC control layer unit testing.
MockMvc realizes the simulation of Http requests. It can directly use the form of network and convert to the call of Controller. This can make the test fast and independent of the network environment. It also provides a set of verification tools, which can make the verification of requests uniform and convenient.
Examples of MockMvc Unit Testing
MockMvc testing process:
1. Create Request
2. Setting parameters (MockMvc can actually set a lot of parameters in this step, but in this case, it's only a simple parameter setting, because the methods are simpler and there are no complex calls)
3. mockMvc calls perform and controller s'business processing logic
4. Performance returns Result Actions, returns operation results, and provides a unified verification method through Result Actions.
The test code is also uploaded to github in the test package and can be downloaded to run the test locally.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath*:/applicationContext.xml", "classpath*:/spring-mvc.xml", "classpath*:/mybatis-config.xml"})
public class BookControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@Test
public void testList() throws Exception {
//Request to create a list of books
//The way to request is get
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get("/book/listAll.do");
//This request does not need to add the request parameter mockMvc. perform (mockHttpServletRequestBuilder). and Expect (status (). isOk ())
.andDo(print());
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath*:/applicationContext.xml", "classpath*:/spring-mvc.xml", "classpath*:/mybatis-config.xml"})
@TransactionConfiguration(defaultRollback = false)
@Transactional
public class StoreControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@Test
public void testSave() throws Exception {
//Request for bookshelf creation
//The request is post
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post("/store/save.do");
//Add bookshelves numbered MockMvc
mockHttpServletRequestBuilder.param("number", "MockMvc");
//The bookshelf has two layers.
mockHttpServletRequestBuilder.param("level", "2");
mockMvc.perform(mockHttpServletRequestBuilder).andExpect(status().isOk())
.andDo(print());
}
@Test
public void testList() throws Exception {
//Request for bookshelf creation
//The request is post
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.post("/store/list.do");
//Some of the parameters I commented out, you can add the relevant parameters to get different test results.
//Records with status 0
//mockHttpServletRequestBuilder.param("status", "0");
//Records with bookshelf number dd
//mockHttpServletRequestBuilder.param("number", "dd");
//First page
mockHttpServletRequestBuilder.param("page", "1");
//10 records per page
mockHttpServletRequestBuilder.param("rows", "10");
mockMvc.perform(mockHttpServletRequestBuilder).andExpect(status().isOk())
.andDo(print());
//The console prints the following results:
//MockHttpServletResponse:
//Status = 200 corresponds to the success of the back end
//Return data
}
}
summary
If you are still used to starting tomcat server, and then enter the address test in the browser, it is also necessary. The method has been written on it, so it depends on personal habits.
The above tests are all simple tests for this project. They should not be very complex and easy to use. If there are complex tests later, I will give a further explanation. If I want to know more about the MockMvc test of Spring MVC, I can search the relevant tutorials by myself.