簡體   English   中英

junit5 沒有接受測試

[英]junit5 is not picking up tests

我有簡單的測試

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 public class MyTestClass{

    private static final Logger logger = LoggerFactory.getLogger(MyTestClass.class);

    private static final Long timeout = 5000L;

    @Test
    public void test(){
        Assertions.assertTrue(true);
    }
}

我嘗試用mvn test -Dtest=MyTestClass#test test運行它,但出現錯誤:

無法在項目 TestProject 上執行目標 org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test(默認測試):未執行任何測試。 (設置 -DfailIfNoTests=false 以忽略此錯誤。)

當我嘗試使用 intellij 運行它時:

發生內部錯誤。 org.junit.platform.commons.JUnitException:ID為'junit-jupiter'的TestEngine未能在org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.882132469458platform:111)在org.828.85834828.8583發現測試launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:85)在org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:92)在org.883898559.Defaultlaunchercore.Defaultunchercore.platform DefaultLauncher.java:75) at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt .junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220) 在 com.intellij.rt.junit.JUnitStarter。 main(JUnitStarter.java:53) Caused by: java.lang.NoClassDefFoundError: org/junit/jupiter/api/ClassOrderer at org.junit.jupiter.engine.config.DefaultJupiterConfiguration.(DefaultJupiterConfiguration.java:50) at org.junit .jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:66) at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:103)... 7 more Caused by: java.lang.ClassNotFoundException: org.junit.jupiter.api.ClassOrderer at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass( Launcher.java:355) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:351)... 10 更多

進程結束,退出代碼為 -2

我的依賴如下:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <dependencies>
        </dependencies>
        <configuration>
            <trimStackTrace>false</trimStackTrace>
            <printSummary>true</printSummary>
            <excludes>
                <exclude>integration/*.java</exclude>
            </excludes>
        </configuration>
    </plugin>

   <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>3.11.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>3.11.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

為什么 maven 和 intellij 都不能執行 Junit5 測試? 解決這個問題的方法是什么? surefire 版本應該已經支持 JUnit5 測試。

<includes>*</includes>到 surefire 配置中。

我有同樣的問題,包括所有明確的幫助。 我正在使用 surefire 3.0.0-M3運行此配置。

嘗試升級到5.8.2 - 我確實在一夜之間發生了同樣的問題,沒有任何變化,升級到最新的 Jupiter 版本為我解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM