当我尝试在 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
原文由 Bhuwan Prasad Upadhyay 发布,翻译遵循 CC BY-SA 4.0 许可协议
将 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
。如果你按照那个例子,你最终会得到类似下面的东西。要 编写 测试,您只需要
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 的非测试版,那么您肯定也希望JupiterTestEngine
在 IDE 的类路径中。问候,
Sam( _核心 JUnit 5 提交者_)