操作系統:window10
開發工具:Spring Tool Suite - Eclipse Oxygen.3a (4.7.3a)
springboot版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
使用 Spring Starter Project 快速方式 創建的項目,自帶的測試類
右鍵Run as--> Junit Test ,執行報錯 no tests found with test runner 'Junit5'
查看控制檯輸出
java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.<init>(JUnit5TestLoader.java:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:438)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createRawTestLoader(RemoteTestRunner.java:367)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.createLoader(RemoteTestRunner.java:362)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.defaultInit(RemoteTestRunner.java:306)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.init(RemoteTestRunner.java:221)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:205)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.core.LauncherFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 11 more
網上搜索各種帖子,給出的解決方案 很多:讓 修改 junit的版本到4、在@SpringBootTest 後面關聯上 啓動類、添加@Runwith註解等等,均沒有用。
最後看到了一篇分析貼,有段知識挺好,分享給大家:
JUnit Platform是提供了運行(測試框架)環境的平臺,JUnit Jupiter 是新的Junit5(子項目提供了一個基於平臺測試運行Jupiter的測試引擎),JUnit Vintage提供了Junit3/4的測試引擎(應該是向前兼容的意思)
最後,在一個外文網站上找到了解決方案:
You are missing JUnit 5 platform launcher with group: 'org.junit.platform', name: 'junit-platform-launcher' .Gradle dependency: testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.1.1'
添加上 junit-platform-launcher 對應的依賴
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
</dependency>
再次執行,成功!