'Junit (4.12) is not executing after spring-boot 2.6.2 migration
I have migrated from Spring to Spring-boot version 2.6.2. mvn clean install is successful but none of the junit(version 4.12) is executing. After few research I got to know that Spring-boot 2.4 onwards, JUnit4 has been removed. I tried below solutions which didn't work.
Solution 1:[1]
I think you ought to migrate to JUnit 5.
Solution 2:[2]
Exclude the junit-jupiter-engine and junit-vintage-engine from the spring-boot-starter-test dependency and then add JUnit 4 dependency in your pom:
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Solution 3:[3]
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage.
Refer to: Junit5 doc
If you import junit-jupiter by depending on latest spring-boot-starter-test, it will only run the Junit5 style test cases. So Junit provides junit-vintage to run old Junit3/Junit4 cases and by default it's not contained in the spring-boot-starter-test dependencies. So there are two solutions to keep running Junit4:
Depend on both of junit-jupiter and junit-vintage to support all Junit3/4/5 cases.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <scope>test</scope> </dependency>
Exclude junit-jupiter from spring-boot-starter-test to run Junit4
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> </exclusion> </exclusions> </dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sergey Tsypanov |
Solution 2 | Ilze |
Solution 3 | Yun-tao Yang |