Zug*_*alt 387 java junit junit4 maven-3 maven
我有一个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;
import org.junit.Test;
public class ClassUnderTestTests {
private ClassUnderTest o;
@Before
public void setUp() {
o = new ClassUnderTest();
}
@Test
public void testFunctionUnderTest_testCase1() {
Assert.assertEquals(1, o.functionUnderTest(1));
}
@Test
public void testFunctionUnderTest_testCase2() {
Assert.assertEquals(2, o.functionUnderTest(2));
}
}
Run Code Online (Sandbox Code Playgroud)
mvn -X测试结束:
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-surefire-plugin:2.7.1, parent: sun.misc.Launcher$AppClassLoader@5224ee]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test' with basic configurator -->
[DEBUG] (s) basedir = /Users/aaron/Programs/my_program
[DEBUG] (s) childDelegation = false
[DEBUG] (s) classesDirectory = /Users/aaron/Programs/my_program/target/classes
[DEBUG] (s) disableXmlReport = false
[DEBUG] (s) enableAssertions = true
[DEBUG] (s) forkMode = once
[DEBUG] (s) junitArtifactName = junit:junit
[DEBUG] (s) localRepository = id: local
url: file:///Users/aaron/.m2/repository/
layout: none
[DEBUG] (f) parallelMavenExecution = false
[DEBUG] (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.7.1:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.7.1:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.7.1:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.0.5:compile, junit:junit=junit:junit:jar:3.8.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
[DEBUG] (s) printSummary = true
[DEBUG] (s) project = MavenProject: my_group:my_program:1.0-SNAPSHOT @ /Users/aaron/Programs/my_program/pom.xml
[DEBUG] (s) projectArtifactMap = {junit:junit=junit:junit:jar:4.8.1:test}
[DEBUG] (s) redirectTestOutputToFile = false
[DEBUG] (s) remoteRepositories = [ id: central
url: http://repo1.maven.org/maven2
layout: default
snapshots: [enabled => false, update => daily]
releases: [enabled => true, update => never]
]
[DEBUG] (s) reportFormat = brief
[DEBUG] (s) reportsDirectory = /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@dfbb43
[DEBUG] (s) skip = false
[DEBUG] (s) skipTests = false
[DEBUG] (s) testClassesDirectory = /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] (s) testFailureIgnore = false
[DEBUG] (s) testNGArtifactName = org.testng:testng
[DEBUG] (s) testSourceDirectory = /Users/aaron/Programs/my_program/src/test/java
[DEBUG] (s) trimStackTrace = true
[DEBUG] (s) useFile = true
[DEBUG] (s) useManifestOnlyJar = true
[DEBUG] (s) workingDirectory = /Users/aaron/Programs/my_program
[DEBUG] -- end configuration --
[INFO] Surefire report directory: /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] Setting system property [user.dir]=[/Users/aaron/Programs/my_program]
[DEBUG] Setting system property [localRepository]=[/Users/aaron/.m2/repository]
[DEBUG] Setting system property [basedir]=[/Users/aaron/Programs/my_program]
[DEBUG] Using JVM: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-junit4:jar:2.7.1:test (selected for test)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:test (selected for test)
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.7.1/surefire-junit4-2.7.1.jar Scope: test
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: test
[DEBUG] Test Classpath :
[DEBUG] /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] /Users/aaron/Programs/my_program/target/classes
[DEBUG] /Users/aaron/.m2/repository/junit/junit/4.8.1/junit-4.8.1.jar
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
Forking command line: /bin/sh -c cd /Users/aaron/Programs/my_program && /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /Users/aaron/Programs/my_program/target/surefire/surefirebooter6118081963679415631.jar /Users/aaron/Programs/my_program/target/surefire/surefire4887918564882595612tmp /Users/aaron/Programs/my_program/target/surefire/surefire9012255138269731406tmp
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.089s
[INFO] Finished at: Mon May 30 12:03:09 EDT 2011
[INFO] Final Memory: 7M/62M
[INFO] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
axt*_*avt 630
默认情况下,Maven在查找要运行的测试时使用以下命名约定:
Test**Test*TestCase您的测试类不遵循这些约定.您应该重命名它或配置Maven Surefire插件以使用其他模式进行测试类.
小智 85
我还发现单元测试代码应该放在测试文件夹下,如果你将它放在主文件夹下,它就不能被识别为测试类.例如.
错误
/my_program/src/main/java/NotTest.java
Run Code Online (Sandbox Code Playgroud)
对
/my_program/src/test/java/MyTest.java
Run Code Online (Sandbox Code Playgroud)
Jon*_*Jon 65
如果模块的包装没有正确声明,另一件事可能导致Maven找不到测试.
在最近的一个案例中,有人<packaging>pom</packaging>和我的测试从未运行过.我把它改成了<packaging>jar</packaging>,现在它工作正常.
tey*_*zer 48
更新:
就像@scottyseus在评论中所说,从Maven Surefire 2.22.0开始,以下就足够了:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
Run Code Online (Sandbox Code Playgroud)
当使用JUnit 5时,我遇到了同样的问题.Maven Surefire需要一个插件来运行JUnit 5测试.将此添加到我们的pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0-M1</version>
</dependency>
</dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)
资料来源:https: //junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
Ext*_*r13 33
如果您Spring Boot使用Spring Initializr创建了一个应用程序,则测试在 Intellij Idea 中运行良好。但是,如果尝试从命令行运行测试:
mvn clean test
Run Code Online (Sandbox Code Playgroud)
您可能会感到惊讶,根本没有运行任何测试。我试图添加但surefire plugin没有运气。答案很简单:pom.xml包含以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
排除项junit-vintage-engine专用于保持与 的向后兼容性JUnit 4.x。因此,默认情况下,新版本的 Spring Boot Initializr 不支持它。删除排除项后,Maven开始查看项目的测试。
t3r*_*n41 30
另外,检查测试类目录(例如src/test/java)是否对应于属性<testSourceDirectory>下pom.xml 中属性中列出的目录<build>.花了一段时间才发现这一点.
edu*_*nti 16
我正在努力解决这个问题。就我而言,我没有导入正确的@Test注释。
1) 检查@Test 是否来自org.junit.jupiter.api.Test(如果您使用的是 Junit 5)。
2) 使用 Junit5 而不是@RunWith(SpringRunner.class),使用@ExtendWith(SpringExtension.class)
import org.junit.jupiter.api.Test;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application.properties")
public class CotacaoTest {
@Test
public void testXXX() {
}
}
Run Code Online (Sandbox Code Playgroud)
rob*_*ins 11
如果项目有Maven,Maven将不会运行您的测试 <packaging>pom</packaging>
您需要将包装设置为jar(或其他一些java artefact类型)才能运行测试: <packaging>jar</packaging>
Joã*_*tos 10
过去,这些答案中的许多对我很有用,但是我想添加一个额外的方案,该方案花费了我一些时间,因为将来可能会对其他人有所帮助:
确保测试类和方法是公共的。
我的问题是我在使用IDE(IntelliJ)的自动测试类/方法生成功能,由于某种原因,它将它们创建为程序包专用。我发现这比人们期望的更容易错过。
检查一下(对于jUnit - 4.12和Eclipse surefire插件)
如果您使用"Abstract"为测试添加前缀,则默认情况下它将被忽略.
如果您的测试类名称不遵循标准命名约定(如上面 @axtavt 突出显示的那样),您需要在 中添加模式/类名称,pom.xml以便 Maven 选择测试 -
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*_UT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
...
Run Code Online (Sandbox Code Playgroud)
小智 5
我也有类似的问题,经过探索发现 testng 依赖导致了这个问题。从 pom 中删除 testng 依赖项后(因为我不再需要它了),它开始对我工作正常。
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这是我必须添加到 pom.xml 的确切代码:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
这是我的依赖项:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<version>2.0M10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0-M1</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
在我的情况下,它添加了junit-vintage-engine,使其与旧版本的JUnit测试兼容并可以运行它们。当我使用JUnit 5时。
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
以下内容在 Junit 5 中对我来说效果很好
https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
<!-- ... -->
Run Code Online (Sandbox Code Playgroud)
就我而言,我们将多模块应用程序迁移到 Spring Boot。不幸的是,maven 不再在模块中执行所有测试。测试类的命名没有改变,我们遵循命名约定。
最后,当我将依赖项添加surefire-junit47到插件时,它有所帮助maven-surefire-plugin。但我无法解释,为什么,这是反复试验:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${maven-surefire-plugin.version}</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
Maven Surefire 插件支持多种测试框架。它会尝试自动检测您正在使用的框架,然后查找使用该框架编写的测试。如果自动检测被混淆,并选择了错误的框架,第二阶段将不会找到您的测试。
自动检测的工作原理是扫描类路径以查找其支持的测试框架是否存在重要的“驱动程序”类。因此,如果您的 POM 或依赖的模块对这些“驱动程序”类之一具有不正确的依赖关系,则自动检测可能会出错。
目前(2020年),一个特殊的问题是JUnit 4和JUnit 5之间的区别。 Surefire插件将它们视为不同的框架。但是由于包名的相似性,一个项目可能依赖于错误的框架,但在不经意间检查似乎没问题。
特别要注意,它junit-platform-console是针对 JUnit 5 的,但junit-platform-runner针对的是 JUnit 4。如果您的项目依赖于后者,Surefire 将不会运行您的 JUnit 5 测试。
| 归档时间: |
|
| 查看次数: |
217827 次 |
| 最近记录: |