如果没有至少一个TestEngine,则无法创建启动器;考虑在Junit 5中将引擎实现JAR添加到类路径中 [英] Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath in Junit 5
问题描述
当我尝试在junit5中运行测试用例时,我得到了执行:
I got following execption when i tried to run test case in 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()
);
}
}
目标: mvn test
推荐答案
对于初学者,您将 ALPHA 快照工件(即org.junit:junit5-api:5.0.0-SNAPSHOT
)与 M2 工件混合在一起(即org.junit.platform:junit-platform-surefire-provider:1.0.0-M2
),它将永远无法正常工作.
For starters, you are mixing ALPHA snapshot artifacts (i.e., org.junit:junit5-api:5.0.0-SNAPSHOT
) with M2 artifacts (i.e., org.junit.platform:junit-platform-surefire-provider:1.0.0-M2
), which will never work.
Maven 部分建议查看 junit5-maven-consumer 项目.如果遵循该示例,最终将得到类似以下的内容.
The Maven section in the user guide suggests to check out the pom.xml
from the junit5-maven-consumer project. If you follow that example, you will end up with something like the following.
<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
.
To write your tests, you only need the junit-jupiter-api
; however, in order to run your tests you must have a TestEngine
on the classpath. For JUnit Jupiter you therefore need junit-jupiter-engine
on the classpath as well.
正如Nicolai Parlog所指出的,您可以添加junit-jupiter-engine
作为maven-surefire-plugin
的依赖项.但是,这不会在您的IDE的类路径中包含JupiterTestEngine
.
As Nicolai Parlog pointed out, you could add junit-jupiter-engine
as a dependency for the maven-surefire-plugin
; however, that would not include the JupiterTestEngine
in the classpath for your IDE.
如果仅通过Maven或最新版本的IntelliJ 2016(具有对JUnit 5的内置支持)运行测试,则您可能不在乎JupiterTestEngine
是否在IDE的类路径中.但是...如果您使用的是Eclipse,NetBeans或IntelliJ的非beta版本,则肯定也要在IDE的类路径中使用JupiterTestEngine
.
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 JupiterTestEngine
is 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 JupiterTestEngine
on the classpath in the IDE as well.
此致
Sam(核心JUnit 5提交者)
这篇关于如果没有至少一个TestEngine,则无法创建启动器;考虑在Junit 5中将引擎实现JAR添加到类路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!