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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38993446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:59:58  来源:igfitidea点击:

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

javamavenjunit5

提问by Bhuwan Prasad Upadhyay

I got following execption when i tried to run test case in junit5:

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

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>

Test 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 Goal:mvn test

马文目标:mvn test

回答by Sam Brannen

For starters, you are mixing ALPHAsnapshot artifacts (i.e., org.junit:junit5-api:5.0.0-SNAPSHOT) with M2artifacts (i.e., org.junit.platform:junit-platform-surefire-provider:1.0.0-M2), which will never work.

首先,您将ALPHA快照工件(即org.junit:junit5-api:5.0.0-SNAPSHOT)与M2工件(即org.junit.platform:junit-platform-surefire-provider:1.0.0-M2)混合在一起,这将永远不会起作用。

The Mavensection in the user guide suggests to check out the pom.xmlfrom the junit5-maven-consumerproject. If you follow that example, you will end up with something like the following.

Maven的用户手册中的部分建议对检查出pom.xmljunit5-Maven的消费项目。如果你按照那个例子,你最终会得到类似下面的东西。

<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>

To writeyour tests, you only need the junit-jupiter-api; however, in order to runyour tests you must have a TestEngineon the classpath. For JUnit Jupiter you therefore need junit-jupiter-engineon the classpath as well.

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

As Nicolai Parlog pointed out, you could add junit-jupiter-engineas a dependency for the maven-surefire-plugin; however, that would not include the JupiterTestEnginein the classpath for your IDE.

正如 Nicolai Parlog 指出的那样,您可以添加junit-jupiter-enginemaven-surefire-plugin; 但是,这不会JupiterTestEngine在您的 IDE 的类路径中包含。

If you're only running tests via Maven or with a recent beta version of IntelliJ 2016 (which has built-in support for JUnit 5), then you may not care if JupiterTestEngineis on the classpath in your IDE. But... if you're using Eclipse, NetBeans, or a non-beta version of IntelliJ, you'll definitely want the JupiterTestEngineon the classpath in the IDE as well.

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

Regards,

问候,

Sam (core JUnit 5 committer)

Sam(核心 JUnit 5 提交者

回答by Nicolai

The Maven Surefire plugin not only needs the JUnit 5 provider but also a TestEngineimplementation to run tests with. To quote the JUnit 5 docs:

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

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

为了让 Maven Surefire 运行任何测试,TestEngine必须在运行时类路径中添加一个实现。

In accordance with that the following works:

据此,以下工作:

<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>

Note that this configuration makes the engine a dependency of the surefire plugin, notof your test code.

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

回答by FabianoLothor

I solve it changing

我解决它改变

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

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

to

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

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