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:
1 2 3 4 5 |
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> |
Secondly, we need to include the spring-boot-starter-test dependency:
1 2 3 4 5 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> |
Next, let’s see Maven dependencies of JUnit 5:
1 2 3 4 5 6 7 8 |
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> </dependency> |
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:
1 2 3 4 5 6 7 8 9 |
@RestController public class HelloController { @GetMapping("/about") public String aboutMe() { return "JUnit 5 and Spring Boot Example."; } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class ApplicationTests { @Autowired private TestRestTemplate restTemplate; @Test public void testAbout() { String message = this.restTemplate.getForObject("/about", String.class); assertEquals("JUnit 5 and Spring Boot Example.", message); } } |
We can observe that the test class is annotated with the @ExtendWith annotation where its value is the SpringExtension.class:
1 |
@ExtendWith(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:
1 2 |
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; |
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:
1 |
mvn test |
The output will be:
1 2 3 4 5 6 7 8 9 |
Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.921 s [INFO] Finished at: 2017-09-12T22:46:23+07:00 |
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 Disable or Ignore A Test
JUnit 5 Dynamic Tests – Generate Tests at Run-time
JUnit 5 Test Suite – Aggregating Tests In Suites
Display Names and Technical Names in JUnit 5