无法创建没有测试引擎的启动器;考虑在Junit 5中将引擎实现JAR添加到类路径中。

67

当我尝试在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>

测试类:

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

5个回答

46

混合使用ALPHA快照构件(即org.junit:junit5-api:5.0.0-SNAPSHOT)和M2构件(即org.junit.platform:junit-platform-surefire-provider:1.0.0-M2),是行不通的。

用户指南中的Maven部分建议查看来自junit5-maven-consumer项目的pom.xml。如果您按照该示例操作,则最终会得到以下结果。

<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 的依赖项;但是,这样不会将 JupiterTestEngine 包含在 IDE 的类路径中。

如果您只通过 Maven 或最近的 IntelliJ 2016 beta 版本(内置对 JUnit 5 的支持)运行测试,则可能不需要在 IDE 的类路径中添加 JupiterTestEngine。但是...... 如果您正在使用 Eclipse、NetBeans 或非 beta 版本的 IntelliJ,则绝对需要在 IDE 的类路径中添加 JupiterTestEngine

敬礼,

Sam(核心 JUnit 5 贡献者)


27

因在使用Gradle和JUnit 4时遇到相同的错误,最终到了这里。在此情况下的解决方案可以在文档中找到:

只要您在JUnit 4上配置了testImplementation依赖项和类似于以下内容的JUnit Vintage TestEngine testRuntimeOnly依赖项,JUnit平台就可以运行基于JUnit 4的测试。

还需要一个测试引擎:

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

12

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

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

12

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

  

为了使Maven Surefire运行任何测试,必须将TestEngine实现添加到运行时类路径中。

根据这一点,以下内容有效:

<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插件的依赖项,而不是你的测试代码的依赖项。


0

我解决了这个问题,将

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

改为

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接