我有一个maven程序,它编译得很好.当我运行mvn test它时不会运行任何测试(在TESTs标题下说There are no tests to run.).
我用一个超级简单的设置重新创建了这个问题,我将在下面包含以及运行时的输出-X.
单元测试从eclipse运行良好(两者都使用默认的junit包,当我改为包含maven下载的junit.jar时).mvn也test-compile正确地在test-classes下创建了类.我在OSX 10.6.7上使用Maven 3.0.2和java 1.6.0_24运行它.
这是目录结构:
/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java
Run Code Online (Sandbox Code Playgroud)
pom.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my_group</groupId>
<artifactId>my_program</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>My Program</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
ClassUnderTest.java:
public class ClassUnderTest {
public int functionUnderTest(int n) {
return n;
}
}
Run Code Online (Sandbox Code Playgroud)
ClassUnderTestTests.java:
import org.junit.Assert;
import org.junit.Before; …Run Code Online (Sandbox Code Playgroud) 当我试图在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)
Run Code Online (Sandbox Code Playgroud)
的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> …Run Code Online (Sandbox Code Playgroud) Mail Failsafe插件在我运行命令时找不到我的JUnit 5集成测试mvn clean failsafe:integration-test,尽管它可以找到文件.
我有junit-jupiter-api和junit-jupiter-engine作为测试依赖项:
<properties>
<junit.jupiter.version>5.0.1</junit.jupiter.version>
</properties>
<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>
Run Code Online (Sandbox Code Playgroud)
我的集成测试是正确命名(下面的**/*IT.java,**/IT*.java或者**/*ITCase.java,其中包括由故障安全默认值,由神火排除默认情况下).
有什么办法可以在Failsafe中使用JUnit 5测试吗?
考虑以下测试类:
public class SomeClass {
@Autowired
private SomeDependency someDependency;
public int inc(int i) {
someDependency.doSomething();
return i + 1;
}
}
Run Code Online (Sandbox Code Playgroud)
如何someDependency在JUnit 5(5.0.1)测试中为Spring Boot 2(2.0.0.M2)应用程序模拟(最好使用Mockito)?当我尝试调用SomeClass#inc(int)它时会产生一个NullPointerException因为没有注入自动连接的依赖项.
我是 IDEA 和 Gradle 生态系统的新手,所以请原谅我提出的基本问题(如果有的话)。
IntelliJ官方宣布支持JUnit 5已经快2年了,不知为何,IDEA仍然讨厌JUnit 5。如果按照官方说明添加JUnit支持,IDEA默认为JUnit 4.12。
无论如何,在阅读之后,我认为我已经在 build.gradle 中正确设置了依赖项:
dependencies {
testCompile("org.junit.platform:junit-platform-launcher:1.2.0")
testCompile("org.junit.jupiter:junit-jupiter-engine:5.2.0")
testCompile("org.junit.vintage:junit-vintage-engine:5.2.0")
}
Run Code Online (Sandbox Code Playgroud)
我的类路径中有所有这些。另外,我发现自己在我的类路径中手动添加了opentest4j-1.1.0.jar。
我按照这些说明为 JUnit 创建了一个新的运行配置。测试确实运行成功,但随着测试运行器的输出,我得到了很多异常,如下所示。难道我做错了什么?
"C:\Program Files\Java\jdk-10.0.1\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\lib\idea_rt.jar=57664:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\plugins\junit\lib\junit-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.1.5\plugins\junit\lib\junit5-rt.jar;C:\Users\anurag.x.bhandari\IdeaProjects\HackerRankCodingInterview\out\test\Solutions;C:\Users\anurag.x.bhandari\IdeaProjects\HackerRankCodingInterview\out\production\Solutions;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.jupiter\junit-jupiter-api\5.2.0\9cd901df48d88d8e605a6ccb2c3f140c92db6bf2\junit-jupiter-api-5.2.0.jar;C:\Users\anurag.x.bhandari\IdeaProjects\HackerRankCodingInterview\libs\opentest4j-1.1.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.platform\junit-platform-launcher\1.2.0\ea230ba8c0b71943d4dd2bb215ca29041c89966e\junit-platform-launcher-1.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.jupiter\junit-jupiter-engine\5.2.0\de87318ccd3dfa1a98ebfef792d362776f1914de\junit-jupiter-engine-5.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.platform\junit-platform-commons\1.2.0\dbce1d822d3dc6c61703b340cd79018518685451\junit-platform-commons-1.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.vintage\junit-vintage-engine\5.2.0\ff11a5d0674df5e2264e50fe3f8a3485a4399ccb\junit-vintage-engine-5.2.0.jar;C:\Users\anurag.x.bhandari\.gradle\caches\modules-2\files-2.1\org.junit.platform\junit-platform-engine\1.2.0\35fa3529ce843ada1a10b0909ccb4a8148ee638d\junit-platform-engine-1.2.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 @w@C:\Users\anurag.x.bhandari\AppData\Local\Temp\idea_working_dirs_junit.tmp @C:\Users\anurag.x.bhandari\AppData\Local\Temp\idea_junit.tmp -socket57663
Jun 21, 2018 9:26:48 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed …Run Code Online (Sandbox Code Playgroud)