1. Test-driven development (testing driven DEVELOPMENT,TDD) is a test case where we write our own expected results as required, and this test case is just beginning to be a failed test, with constant coding and refactoring, Finally, test cases are tested so that the quality and controllability of the software can be guaranteed. 2. Test dependencies, the package survives the test cycle, and the release does not contain jar packages.
1 <Dependency>2 <groupId>Org.springframework.boot</groupId>3 <Artifactid>Spring-boot-starter-test</Artifactid>4 <Scope>Test</Scope>5 </Dependency>
3. Main notes of Spring test:
(1) @RunWith (Springrunner.class) to run the test in a spring environment.
(2) @SpringBootTest represents a Springboot test.
(3) @AutoConfigureMockMvc representative injects a MOCKMVC instance, Mockmvc simulates the MVC object.
(4) @WebAppConfiguration to declare that ApplicationContext will be a webapplicationcontext.
(5) @Autowired inject an instance into the test case.
(6) @Test is the method of testing.
(7) @Before the initialization done before the test begins.
(8) @After The method of operation that is performed after the test method.
4, the service layer of testing:
(1) The service layer test class requires only the addition of @runwith (Springrunner.class) and @springboottest two annotations.
(2) Use @autowired annotation to inject service object, when service throws Java.lang.exception, throws exception after test method.
(3) Note that the Save method is a parameter, such as a method parameter is an entity entities, the Set method can be used to add property values, to achieve the parameters passed. If a number of parameters, directly to the parameter assignment in the method can be. All parameters that include the ID value within a record must have a value.
5, Controller layer test:
(1) Controller layer test class need to add @runwith (Springrunner.class), @SpringBootTest, @ AUTOCONFIGUREMOCKMVC and webappconfiguration four annotations.
(2) Use @autowired annotation to inject controller object, when controller throws Java.lang.exception, throws exception after test method.
(3) First execute command mockmvcbuilders.standalonesetup (qcqpbcinematicketcontroller). Build () specifies the controller for the test.
(4) Comment out the permissions of the method that needs to be tested, otherwise org.springframework.web.util.NestedServletException:Request processing failed request fails.
(5) The Controller layer test class URL needs to be consistent with the method URL format. For example, when you test the controller layer's list method, the general URL is written to the Director class map plus method mapping, but after the page constraints, the URL needs to be changed to url?page=1&limit=1000&sidx=& Order=.
(6) When a startup item is required to get a value to pass as a parameter, the test reports Org.springframework.web.util.NestedServletException:Request processing failed. This was an invalid application configuration. Because the test method does not get the value, you need to comment out the command that starts the project to get the value, and then test it to achieve the effect you want to test.
(7) When the test method needs to pass the parameter, use the params to pass the set parameters, but the keys and values are string types, if the controller layer method to pass the entity class parameters, it does not affect. But to pass different types of arguments, you need to convert the string type to the appropriate parameter type.
(8) When the ID value is increased, adding the data ID value without changing the ID value is normal self-increment. To change the ID value in the test method pass ID parameter is not feasible, you need to customize an ID value in Serviceimpl, so it is more troublesome.
(9) Controller layer testing also has its own limitations. If the controller layer method needs to pass the entity class arguments, the test method passes the collection parameter, and the server reports a 400 error. The solution is to test at the service level.
Spring test component for unit testing