首页 > 解决方案 > junit5 没有进行测试

问题描述

我有简单的测试

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 (default-test):未执行任何测试!(设置 -DfailIfNoTests=false 以忽略此错误。)

当我尝试使用 intellij 运行它时:

发生内部错误。org.junit.platform.commons.JUnitException:ID 为“junit-jupiter”的 TestEngine 未能在 org.junit.platform 的 org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:111) 中发现测试。 launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:85) at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:92) at org.junit.platform.launcher.core.DefaultLauncher.execute( DefaultLauncher.java:75) 在 com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71) 在 com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) 在 com.intellij.rt .junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220) 在 com.intellij.rt.junit.JUnitStarter。

进程以退出代码 -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 测试。

标签: javamavenintellij-ideajunit

解决方案


添加<includes>*</includes>到万无一失的配置。

我有同样的问题,包括所有明确的帮助。我正在使用 surefire 运行此配置3.0.0-M3


推荐阅读