JUnit is the testing framework that Spring Boot uses by default, and the spring-boot-starter-test starter adds it to the project. With Spring Boot 3, the starter brings JUnit 5, so we can write unit tests and integration tests for any Java application without declaring a separate JUnit dependency.
The sections below show which JUnit version each Spring Boot release includes, where the test classes go and how to run them, followed by a demo with a REST controller.
1. Maven
By default, the latest spring-boot-starter-test dependency imports the JUnit 5 dependencies into the Spring Boot application.
The JUnit version that Spring Boot includes changed in the following releases.
- Prior to Spring Boot v2.2.0, by default, JUnit 4 was imported.
- From Spring Boot v2.2.0 to Spring Boot 2.4.0, JUnit 5 was included by default, and the JUnit-vintage module was included for backward compatibility. [Release Notes v2.2]
- Since Spring Boot 2.4.x, the JUnit 5 is included without the vintage engine. [Release Notes v2.4]
We should migrate JUnit 4 tests to JUnit 5. If the migration needs more time, we can import the JUnit vintage dependency until the migration is finished.
This tutorial uses Spring Boot 3.1.2, which includes JUnit 5 by default.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.2</version>
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
...
</dependencies>
2. Writing JUnit 5 Tests
We write the test classes and methods with the JUnit 5 annotations. For example, the annotation @SpringBootTest loads the Spring Boot application context for integration testing.
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;
@Test
public void testSomeMethod() {
// Your JUnit 5 test logic here
// You can use assertions and other JUnit 5 features
}
}
We write the tests in the /src/test/java/ directory, in a suitable package hierarchy. The Spring Boot Maven plugin runs the tests and provides various testing-related features. For example, it captures and displays the test output, including the test results and any exceptions thrown during testing.
3. Running The Tests
We run the JUnit 5 tests from an IDE or with a build tool such as Maven.
mvn test
4. Demo
The following example is a set of JUnit 5 tests in a Spring Boot 3 application. The tests target a Spring Boot REST controller.
@RestController
@RequestMapping(path = "/employees")
public class EmployeeController {
@Autowired
private EmployeeDAO employeeDao;
@GetMapping(path="/", produces = "application/json")
public Employees getEmployees() {
return employeeDao.getAllEmployees();
}
@PostMapping(path= "/", consumes = "application/json", produces = "application/json")
public ResponseEntity<Object> addEmployee(@RequestBody Employee employee) {
employeeDao.addEmployee(employee);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(employee.getId())
.toUri();
return ResponseEntity.created(location).build();
}
}
The @WebMvcTest annotation is used for a Spring MVC test that focuses on Spring MVC components only. It disables the full auto-configuration and applies only the configuration relevant to MVC tests. It also configures Spring Security and MockMvc.
@WebMvcTest(EmployeeController.class)
public class TestEmployeeRESTController {
@Autowired
private MockMvc mvc;
}
Finally, we use the MockMvc bean to invoke the APIs and verify the results.
@Test
public void getAllEmployeesAPI() throws Exception {
mvc.perform(MockMvcRequestBuilders
.get("/employees")
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.employees").exists())
.andExpect(MockMvcResultMatchers.jsonPath("$.employees[*].employeeId").isNotEmpty());
}
Similarly, we can write the other tests.
5. Conclusion
Spring Boot includes JUnit 5 through the spring-boot-starter-test starter, and the JUnit vintage dependency adds support for JUnit 4 tests that have not been migrated yet. The test classes go in the src/test/java directory, and the demo showed JUnit 5 tests for a REST controller with @WebMvcTest and MockMvc.
Happy Learning !!
Thanks so much. Your website is just awesome and full of treasure content.
junit5v is not supporting powermock , how can we mock private and static methods