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

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

的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测试

解决方法:

对于初学者,您正在将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的依赖项;但是,这不包括IDE的类路径中的JupiterTestEngine.

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

问候,

Sam(核心JUnit 5提交者)

上一篇:java-使用JUnit 5和Spring Boot 2.0.0进行的嵌套测试


下一篇:java-Surefire不排除标有@Tag的JUnit 5测试