avatarlance

Summary

This article discusses two essential tricks for improving the quality of unit testing in Spring Boot projects.

Abstract

Unit testing is a crucial aspect of software development, especially for Java developers using Spring Boot. However, many developers struggle with unit testing in Spring projects, often due to slow loading times caused by the Spring container. This article presents two solutions to this problem: using the spring-boot-test-starter and avoiding Spring in unit test code altogether. The first method involves loading only the necessary Java beans into the Spring container using the @SpringBootTest annotation, which significantly reduces loading times. The second method suggests running unit tests in a MockitoJunit environment, eliminating the need for Spring in unit tests.

Opinions

  • Unit testing is an integral part of software development, allowing developers to identify code defects early and ensuring code correctness.
  • Dependency injection in Spring makes code easier to unit test.
  • Slow Spring container loading times can be a significant hindrance to efficient unit testing.
  • Using the spring-boot-test-starter and loading only necessary Java beans into the Spring container can significantly improve unit testing efficiency.
  • Running unit tests in a MockitoJunit environment can eliminate the need for Spring in unit tests, further improving efficiency.
  • These two tricks can deepen developers' understanding of unit testing in a Spring Boot environment.
  • The article recommends trying out a cost-effective AI service that provides the same performance and functions as ChatGPT Plus(GPT-4) at a lower price.

2 Must-Know Tricks of Unit Testing with Spring Boot

To improve the quality of unit testing of spring projects, you need to know about these tricks

For a java developer, unit testing is an integral part of our coding process. Unit testing allows you to pay attention to the correctness of the basic units of the software, and you can expose code defects in advance at the coding phase, and what’s more, it can make you more calm and confident in code refactoring.

Many people think unit testing is essential, but so many guys can’t do unit testing well, especially unit testing with spring boot. Do you have such a problem in a project with spring integration, Each time you execute a test case, you have to wait for the spring container to load the environment for more than ten seconds and that can be a nightmare for you who pursue efficiency.

Now there are two ways to solve your problem.

1. Use spring-boot-test-starter

spring-boot-test-starter(in the test scope)contains Junit5MockitoSpring Testlibraries and we find these common libraries to be useful when writing tests. Testing with spring environment one of the major advantages is that dependency injection can make your code easier to unit test. Use spring-boot-test-starter, You can just load one or several Java beans you are interested in into the spring container and start testing directly without preparing all the environments. Here is a best practice.

We use the @SpringBootTestannotation, and the attribute is marked withclasses=Demo.class, which means that only the `Demo` class is loaded into the spring container. We just focus on the unit test of this class, and we don’t care about other classes. Is this convenient? It will never affect the execution of unit tests because a database hangs or a resource can’t be loaded, let alone wait for spring to load all beans slowly.

2. Don’t Involve Spring in Your Unit Test Code

Another way is not to use spring in your unit test at all. This way can also solve the problem of slow unit tests caused by slow spring loading environment. Let’s use the above example to reform it like this.

Note that spring is no longer used in the above unit tests. Instead, run the unit tests in the MockitoJunit environment to enable the annotation of mockito.

The above are two Tricks of unit testing with Spring Boot. I hope these two Tricks can deepen your understanding of unit testing based on spring boot environment.

Spring Boot
Unit Testing
Java
Programming
Testing
Recommended from ReadMedium