如果没有至少一个TestEngine,则无法创建启动器; 考虑在Junit 5中将引擎实现JAR添加到类路径中

dev*_*wan 24 java maven junit5

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

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)
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

测试类:

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()
        );
    }

}
Run Code Online (Sandbox Code Playgroud)

Maven目标: mvn test

Sam*_*nen 28

对于初学者,您正在混合ALPHA快照工件(即org.junit:junit5-api:5.0.0-SNAPSHOT)与M2工件(即org.junit.platform:junit-platform-surefire-provider:1.0.0-M2),这将永远不会起作用.

用户指南中的Maven部分建议pom.xmljunit5-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>
Run Code Online (Sandbox Code Playgroud)

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

正如Nicolai Parlog指出的那样,你可以添加junit-jupiter-engine作为依赖maven-surefire-plugin; 但是,这不包括JupiterTestEngineIDE的类路径中.

如果您只是通过Maven或最新的IntelliJ 2016测试版(内置支持JUnit 5)运行测试,那么您可能不关心是否JupiterTestEngine在IDE的类路径中.但是......如果您使用的是Eclipse,NetBeans或IntelliJ的非beta版本,那么您肯定也希望JupiterTestEngine在IDE中使用类路径.

问候,

Sam(核心JUnit 5提交者)


lai*_*avi 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")
}
Run Code Online (Sandbox Code Playgroud)


Ali*_*Ali 14

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

testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
Run Code Online (Sandbox Code Playgroud)


Nic*_*lai 8

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>
Run Code Online (Sandbox Code Playgroud)

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