[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3

This article describes ** "Spring boot 2.2 or higher, compatible with JUnit 5" **. I hope it will be useful for the following purposes!

  1. Update from Spring boot 2.1 or lower to 2.2 or higher
  2. Transfer from JUnit 4 to JUnit 5
  3. When writing JUnit5, I want to exclude JUnit4 in advance

0. Premise

environment

--Java 8 ~ (Spring boot 2.2 and above support Java 8 and 11)

Knowledge that you should know

--JUnit5 is the default from Spring boot 2.2 and above --JUnit4 and JUnit5 are basically incompatible -** Currently JUnit 4 and 5 can coexist (will eventually disappear) **

1. Dependencies for using JUnit 5

Example 1. For Maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Example 2. For Gradle

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}

2. Corrected the description related to JUnit4

--@RunWith has been abolished --Correspondence ... Deleted or changed to @ExtendWith --The import source of @Test has changed --Before support ... ʻimport org.junit.Test; --After support ... ʻimport org.junit.jupiter.api.Test; --@Before has been abolished --Correspondence ... Changed to @BeforeEach or @BeforeAll

(reference)

↓ This is a reference site

Recommended Posts

[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
I wrote a test with Spring Boot + JUnit 5 now
How to write a unit test for Spring Boot 2
Write test code in Spring Boot
Test Spring framework controller with Junit
Perform transaction confirmation test with Spring Boot
Try using Spring Boot with VS Code
Sample code to unit test a Spring Boot controller with MockMvc
Asynchronous processing with Spring Boot using @Async
Let's write a test code for login function in Spring Boot
Form class validation test with Spring Boot
Create a website with Spring Boot + Gradle (jdk1.8.x)
Test controller with Mock MVC in Spring Boot
Create a simple search app with Spring Boot
Create a Spring Boot application using IntelliJ IDEA
Write RestController tests quickly with Spring Boot + Spock
[Java] Hello World with Java 14 x Spring Boot 2.3 x JUnit 5 ~
Test code using mock with JUnit (EasyMock center)
Create a web api server with spring boot
Create a Spring Boot development environment with docker
Unit test with Junit.
Download with Spring Boot
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
Create a portfolio app using Java and Spring Boot
Try using DI container with Laravel and Spring Boot
[Note] Configuration file when using Logback with Spring Boot
Try using OpenID Connect with Keycloak (Spring Boot application)
Implement a simple Rest API with Spring Security with Spring Boot 2.0
[Introduction to Spring Boot] Submit a form using thymeleaf
A memorandum when creating a REST service with Spring Boot
Test field-injected class in Spring boot test without using Spring container
Create a simple demo site with Spring Security with Spring Boot 2.1
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Using Mapper with Java (Spring)
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Try using Spring Boot Security
Create microservices with Spring Boot
Send email with spring boot
A story packed with the basics of Spring Boot (solved)
Let's make a simple API with EC2 + RDS + Spring boot ①
I tried to get started with Swagger using Spring Boot
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Use Spring Test + Mockito + JUnit 4 for Spring Boot + Spring Retry unit tests
I made a simple MVC sample system using Spring Boot
Implement a simple Web REST API server with Spring Boot + MySQL
Write a Reactive server with Micronaut
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR