Write Unit Test for Spring Boot application

When working with applications that use Spring Boot, you will need to write Unit Test for your code. In this tutorial, I will show you how to write Unit Test for the Spring Boot application!

First, I will create a simple Spring Boot application for example:

Write Unit Test for Spring Boot application

This application just does a simple job of printing a message to the console with the content “Hello Khanh. Total of 2 and 3 is 5”. To do this, our application will have 2 classes declared in the Spring container with the following content:

Hello class:

This class defines a method that allows us to pass a name and it will return the string “Hello” plus the name you passed.

Calculation class:

This class allows us to calculate the sum of 2 numbers.

The result when running this program will be as follows:

Write Unit Test for Spring Boot application

Ok, now we will proceed to write Unit Test code for this application!

The idea is that we need to write Unit Tests to test when the application runs, the two beans of Hello and Calculation classes must be initialized in the Spring container and their methods must return the values we want.

To do this, first you need to make sure the dependency of the Spring Boot Test is declared in your pom.xml file. When I created the Spring Boot project using the Spring Tool Suite, it was included as follows:

You may wonder why, when creating the Spring Boot project by default, this Spring Boot Starter Test excludes the junit-vintage-engine library. The reason is because junit-vintage-engine is for JUnit 4 and JUnit 3, we should use the latest version of JUnit as JUnit 5. With dependency declaration for JUnit 5 as follows:

To write Unit Test for the Spring Boot application, you just need to declare in the Test class an annotation of Spring Boot Test @SpringBootTest. For example:

Or:

The SpringBootUnitTestApplicationTests class has been automatically added:

With this @SpringBootTest annotation, the Spring Boot Test will automatically run a Spring container and initialize beans in this Spring container while we run the test.

Bean of which class will be initialized in Spring container depending on our configuration.

If you declare the @ContextConfiguration annotation with value as classes containing bean information definition, only the beans defined in these classes will be initialized.

If you do not declare the @ContextConfiguration annotation, the Spring Boot Test will automatically scan for classes that are declared with the @Configuration annotation and used with the @SpringBootConfiguration annotation to initialize the bean.

In my example, I am using auto component scan to initialize beans in Spring, do not explicitly declare beans, so these beans are used with the @SpringBootConfiguration annotation to initialize.

You can add code tests to test the beans of classes Hello and Calculation must be initialized in SpringBootUnitTestApplicationTests class as follows:

Test for Hello and Calculation classes will be as follows:

If you only want to test the Hello class, you can create a new TestConfiguration class with the following content:

Then use the @ContextConfiguration annotation to declare this TestConfiguration class in to write tests:

In this case, you won’t be able to get the bean of the Calculation class. For example:

The error will happen immediately:

Write Unit Test for Spring Boot application

Add Comment