We often get this error in Eclipse/STS while working with JUnit. As we know JUnit has released JUnit5 after successor of JUnit4. When we create Spring Boot starter project whose version is higher than 2.1.4.RELEASE then by default Spring adds the JUnit5 dependencies.
Step 1:
If you are using Spring Boot then by default Spring adds spring-boot-starter-test dependency like this.
<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>
Here you need to remove or make comment the <exclusions>. Now if you will check in the Maven Dependency you will get JUnit4.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Step 2:
Goto the the package src/test/java and open one of the JUnit test class. You need to change then @Test annotation import from import org.junit.jupiter.api.Test; to import org.junit.Test;
Step 3:
Now open your one of the JUnit test class then right click. Then goto Run As -> Run Configurations. Now Inside the Test tab, there is an option Test runner. Change it to JUnit5 to JUnit4 then click on Run.
Good job buddy
ReplyDelete