Junit (4.12) is not executing after spring-boot 2.6.2 migration

Viewed 4292
3 Answers

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:

  1. 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>
    
  2. 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>
    

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>

I think you ought to migrate to JUnit 5.

Related