没有至少一个 TestEngine 就无法创建 Launcher;考虑将引擎实现 JAR 添加到 Junit 5 中的类路径

Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath in Junit 5

当我尝试在 junit5 中 运行 测试用例时,我得到了以下执行:

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project CRUD-App: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: There was an error in the forked process
org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath
   at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:161)
   at org.junit.platform.launcher.core.DefaultLauncher.<init>(DefaultLauncher.java:52)
   at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:42)
   at org.junit.platform.surefire.provider.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:59)
   at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:286)
   at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:240)
   at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)

pom.xml

<dependencies>
    ...
    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>junit5-api</artifactId>
        <version>5.0.0-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>        
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-M2</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

测试Class:

public class PersonServiceTest {

    final Database database = Database.from("jdbc:h2:mem:" + App.DB_NAME);

    final PersonService personService = new PersonService(database);

    public PersonServiceTest() {
    }

    @Test
    @DisplayName("@Person#insert()")
    public void testInsert() {
        personService.insert(new PersonBuilder()
                .setId(1).setName("Bhuwan")
                .setAddress("KTM")
                .setContactNo("984849").createPerson()
        );
    }

}

Maven 目标: mvn test

Maven Surefire 插件不仅需要 JUnit 5 提供程序,还需要一个 TestEngine 实现来 运行 测试。引用 JUnit 5 docs:

In order to have Maven Surefire run any tests at all, a TestEngine implementation must be added to the runtime classpath.

根据以下作品:

<build>
    <plugins>        
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-M4</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.0.0-M4</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0-M4</version>
        <scope>test</scope>
    </dependency>
</dependencies>

请注意,此配置使引擎成为 surefire 插件的依赖项,而不是 您的测试代码。

混合 ALPHA 快照工件(即 org.junit:junit5-api:5.0.0-SNAPSHOT)与 M2 工件(即 org.junit.platform:junit-platform-surefire-provider:1.0.0-M2) , 不会工作。

Maven section in the user guide suggests to check out the pom.xml from the junit5-maven-consumer 项目。如果你按照这个例子,你最终会得到类似下面的东西。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
    <junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

编写你的测试,你只需要junit-jupiter-api;但是,为了 运行 你的测试你必须在类路径上有一个 TestEngine 。因此,对于 JUnit Jupiter,您还需要在类路径上 junit-jupiter-engine

正如 Nicolai Parlog 指出的那样,您可以将 junit-jupiter-engine 添加为 maven-surefire-plugin 的依赖项;但是,这不会在您的 IDE.

的类路径中包含 JupiterTestEngine

如果您只是 运行通过 Maven 或使用最新的 IntelliJ 2016 测试版(内置对 JUnit 5 的支持)进行测试,那么您可能不关心 JupiterTestEngine 在您 IDE 的类路径中。但是...如果您使用的是 Eclipse、NetBeans 或 IntelliJ 的非测试版,您肯定还需要 IDE 中类路径上的 JupiterTestEngine

此致,

Sam(核心 JUnit 5 提交者

我改了就解决了

classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE"

classpath "org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE"

由于使用 Gradle 和 JUnit 4 时出现同样的错误而在此处结束。这种情况下的解决方案可以在 docs:

中找到

The JUnit Platform can run JUnit 4 based tests as long as you configure a testImplementation dependency on JUnit 4 and a testRuntimeOnly dependency on the JUnit Vintage TestEngine implementation similar to the following.

还需要一个测试引擎:

dependencies {
    testImplementation("junit:junit:4.13.2")
    testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.8.2")
}

只需添加以下依赖项即可解决此问题。

testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'