preface
Many companies have a mandatory requirement to write unit tests. When submitting code, if the single test fails or the unit test does not meet various coverage standards, the code will be refused to merge. Writing unit tests is also a way to ensure code quality.
Junit unit tests
Most Java developers have used Junit to write unit tests.
Introducing dependencies:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
Copy the code
Add the @test annotation to the method:
Add the @test annotation and you’ll have a running icon.
Spring dependency issues
Nowadays, Java development is almost inseparable from Spring. But with Spring, there is a dependency injection problem when writing single tests.
Suppose we have the following Service:
@Service
public class TestService {
public String getString(a) {
return "Hello World!"; }}Copy the code
Direct autowiring in Junit:
public class JunitTest {
@Autowired
private TestService testService;
@Test
public void test(a) { System.out.println(testService.getString()); }}Copy the code
Operation error:
Springboot writes unit tests
In SpringBoot, solving spring dependencies is very simple. Just a few notes it provides:
Introducing dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Copy the code
A spring-boot-starter-test dependency is introduced, which is required to write single tests in Springboot.
Add @runWith (sprinGrunner.class), @Springboottest annotation to the unit test class:
Now you can inject the Spring dependency normally, run the method, and see the output when SpringBoot starts:
If a SpringBoot project is created through Spring Initialize (as explained in the first article in this series), a unit test class is created automatically:
When we write unit tests, we simply inherit this class.
Unit test transaction rollback
Writing unit tests inevitably involves manipulating databases. Sometimes the unit test database is the same as the development database. In order not to affect the data in the database, the operation needs to be rolled back after the single test is completed. This is also very easy to solve in the springboot, you just need to to class inheritance AbstractTransactionalJUnit4SpringContextTests single measured
The database has the following data:
The unit test classes are as follows:
The test method is as follows:
@Test
public void deleteAll(a) {
userRepo.deleteAll();
}
Copy the code
After executing the method, if the transaction does not roll, all data in the User table is cleared.
According to the results, there is no problem with single test operation. Check the database:
Data is also not deleted, indicating that the transaction has been rolled back.
Remove the inheritance AbstractTransactionalJUnit4SpringContextTests class, again deleteAll method:
The data in the database has been deleted
conclusion
This article describes how to write unit tests in SpringBoot. Writing unit tests elegantly involves many things, such as mocks (mockito, MockMvc, etc.), which are not covered in this article for those interested.