1. Introduction

This quick tutorial is going to cover how to set up and run JUnit 5 with Spring Boot applications.

2. Preparation

JUnit 5 has been released a GA version recently. So, it’s still very early for any other frameworks likes Spring Boot can be ready to integrate with it currently. Therefore, in this tutorial, we will try to set up and run an example of JUnit 5 and Spring Boot.

JUnit 5 requires Java 8, we will need to get it be ready for our development environment. If you’re interested in an overview of JUnit 5, please read Introduction To JUnit 5.

3. Maven Dependencies of JUnit 5 and Spring Boot

Firstly, let’s see main Maven dependencies of Spring Boot that supports JUnit 5:

Secondly, we need to include the spring-boot-starter-test dependency:

Next, let’s see Maven dependencies of JUnit 5:

Note that we didn’t include any dependencies of JUnit 4 which the latest version is 4.12. If you’re interested in JUnit 5, you can refer to the followings tutorials for setting up JUnit 5 examples with Maven and Gradle:

The full pom.xml file can be found on my Github project.

3. Sample Code

In this section, we’re going to define a REST API using Spring Boot. And we use JUnit 5 and RestTemplate to test the API.

Firstly, let see a sample of the controller which defines the REST API:

The controller has only one method, accepts the HTTP GET method and returns a string “JUnit 5 and Spring Boot Example”.

Next, let’s get through simple test source code implemented by JUnit 5 and Spring Boot:

We can observe that the test class is annotated with the @ExtendWith annotation where its value is the SpringExtension.class:

The SpringExtension class integrates the Spring TestContext Framework into JUnit 5’s Jupiter programming model and the @ExtendWith annotation allows us to register the extension for the annotated test class.

Other points are the imported packages:

The assertEquals command and the @Test annotation now are both included in the org.junit.jupiter.api package.

Please read JUnit 5 Assertions for asserting with JUnit 5.

4. Running JUnit 5 and Spring Boot Tests

Currently, JUnit 5 is still supported limited by popular IDE such as Eclipse, IntelliJ, etc. Therefore, running JUnit 5 tests with them requires additional steps. However, we can quickly execute the tests using build tools such as Maven or Gradle. To run the tests, we can go to the project directory and run the below command:

The output will be:

5. Conclusions

The tutorial has illustrated how to how to set up and run JUnit 5 and Spring Boot applications. However, as mentioned in the top of the tutorial, JUnit 5 is still in early stage, we can just do some experiment with snapshot builds of Spring Boot until next 2.0 release.

The sample source code can be found on my Github project. It’s a Maven-based project, therefore it will be easy to be imported into any IDE or build and run.

Below are other articles related to JUnit 5 for your references:

JUnit 5 Tutorial

JUnit 5 vs JUnit 4

JUnit 5 Annotations Example

JUnit 5 Disable or Ignore A Test

JUnit 5 Exception Testing

JUnit 5 Dynamic Tests – Generate Tests at Run-time

JUnit 5 Nested Tests Examples

JUnit 5 Test Suite – Aggregating Tests In Suites

Display Names and Technical Names in JUnit 5

 

0 0 vote
Article Rating