idea中springboot使用junit测试报错的问题

问题:
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
    at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
 
 
Process finished with exit code 1
Empty test suite.
 
原因:
    官方建议如果跑Junit5建议使用IDEA 2017.3之后的版本.因为较之前的版本还不支持Junit5; 
 
 
解决方案:
    1】换高版本的idea
    2】换junit4
 
springboot工程如何使用junit4:
1)修改pom.xml
使用springboot启动器创建的工程的pom中默认会带测试启动器spring-boot-starter-test;
spring-boot-starter-test默认依赖Junit5(JUnit Platform + JUnit Jupiter + JUnit Vintage);
需要排除Junit5相关的依赖包:排除junit-jupiter-api,然后再加入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-api</artifactId>
      </exclusion>
   </exclusions>
</dependency>
 
2)创建测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestRedis {
    @Autowired
    private StringRedisTemplate strTemp;
 
    @Test
    public void go(){
        System.out.println("hello");
    }
}
Application为spring工程的启动类
 
 
 
 
 
posted @ 2020-06-16 10:16  L丶银甲闪闪  阅读(4339)  评论(0编辑  收藏  举报