当我试图在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 test
答案 0 :(得分:23)
对于初学者,您正在将 ALPHA 快照工件(即org.junit:junit5-api:5.0.0-SNAPSHOT
)与 M2 工件(即org.junit.platform:junit-platform-surefire-provider:1.0.0-M2
)混合在一起,这将是永远不会工作。
用户指南中的Maven部分建议您查看pom.xml
项目中的junit5-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>
要编写您的测试,您只需要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提交者)
答案 1 :(得分:8)
Maven Surefire插件不仅需要JUnit 5提供程序,还需要TestEngine
实现来运行测试。引用JUnit 5 docs:
为了让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>
请注意,此配置使引擎成为您的测试代码的surefire插件 not 的依赖项。
答案 2 :(得分:0)
我解决了它的变化
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE"
到
classpath "org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE"