Spring boot (XII) unit test JUnit
1、 Introduction
JUnit is an excellent open source Java unit testing framework, and it is also the most popular testing framework with the highest utilization rate. The development tools eclipse and idea have good support for JUnit. JUnit is mainly used for white box testing and regression testing.
JUnit GitHub address: https://github.com/junit-team
2、 JUnit uses
Development environment:
2.1 detect JUnit dependency
For spring boot projects, JUnit framework support has been added by default, which can be found in POM View in XML:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
If JUnit dependency is not added in Maven project, you can manually add it by referring to the above code.
2.2 foundation use
The simple test code is as follows:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SimpleTest {
@Test
public void dotest() {
int num = new Integer(1);
Assert.assertEquals(num,1);
}
}
Run the project by mail in the test class. The effect is as follows:
You can see from the console that the test passed.
2.3 notes
2.3. 1 annotation list
2.3. 2 timeout test
The code is as follows. Just set the timeout attribute for test. The time unit is milliseconds:
2.4 assertion test
Assertion test, that is, expectation test, is the core of unit test, that is, the expression that determines the test result. The assertion method in the assert object:
Code examples are as follows:
@Test
public void dotest() {
String[] string1 = {"1","2"};
String[] string2 = string1;
String[] string3 = {"1","2"};
Assert.assertEquals(string1,string2);
Assert.assertEquals(string2,string3);
Assert.assertSame(string1,string2);
Assert.assertSame(string2,string3); //验证不通过,string2、string3指向的引用不同
}
2.5 web simulation test
In the spring boot project, JUnit can be directly used to test web projects. Spring provides the "testresttemplate" object, which can be used to simulate requests easily.
Web testing requires only two steps:
The example code is as follows:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void getName() {
String name = restTemplate.getForObject("/name",String.class);
System.out.println(name);
Assert.assertEquals("Adam",name);
}
}
The meaning of getforobject means to execute the get request and return the object result. The second parameter sets the return result to string type. More request methods:
2.6 database test
When testing data operation, we don't want the test to pollute the database. We just need to add "@ transactional" to the test class, so that we can test data operation methods without polluting the database.
The example code is as follows:
@Test
@Transactional
public void savetest() {
User user = new User();
user.setName("Adam");
user.setAge(19);
user.setPwd("123456");
userRepository.save(user);
System.out.println("userId:" + user.getId());
Assert.assertTrue(user.getId()>0);
}
The implementation effect is as follows:
We can see that the ID has been found and the test has passed, which indicates that it is normal to add data, but there is no such data in the database.
If "@ transactional" is removed, the database will be inserted normally.
2.7 idea quick start test
You can quickly add test methods in idea. Just right click "goto" in the class to be tested, click "test", select the code you need to test, and click generate. If you are a Windows user, you can use the default shortcut "Ctrl + Shift + T", and the effect is as follows:
After selecting the method, click OK to generate the corresponding test code. Users only need to improve the specific test logic in the framework.