我有一个Maven程序,它可以正常编译。 当我运行mvn test
它不会运行任何测试(在TESTs标头下显示There are no tests to run.
)。
我已经用一个非常简单的设置重新创建了这个问题,在使用-X
时,将在下面以及输出中包括该设置。
单元测试可以从eclipse正常运行(都使用其默认的junit软件包,而当我包括maven下载的junit.jar时)。 而且mvn test-compile
可以在test-classes下正确创建该类。 我在带有Maven 3.0.2和Java 1.6.0_24的OSX 10.6.7上运行此代码。
这是目录结构:
/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java
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>
ClassUnderTest.java:
public class ClassUnderTest {
public int functionUnderTest(int n) {
return n;
}
}
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));
}
}
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] ------------------------------------------------------------------------
#1楼
我还发现,单元测试代码应放在test文件夹下,如果将其放在主文件夹下,则不能将其识别为测试类。 例如。
错误
/my_program/src/main/java/NotTest.java
对
/my_program/src/test/java/MyTest.java
#2楼
如果您拥有一个共享的Java / Groovy应用程序,而您所拥有的只是Groovy单元测试,那么Maven将找不到任何测试。 可以通过在src / test / java下添加一个单元测试来解决此问题。
#3楼
另外,检查您的测试类目录(例如src / test / java)是否对应于pom.xml中<build>
属性下属性<testSourceDirectory>
中列出的目录。 花了我一段时间才能找到那个。
#4楼
检查(对于jUnit-4.12和Eclipse surefire插件)
- 在依赖项的POM.xml中添加所需的jUnit版本。 执行Maven->更新项目以查看在项目中导出的必需jar。
- 测试类位于文件夹src / test / java和此文件夹的子目录下(或基本文件夹可以在config testSourceDirectory中的POM中指定)。 班级名称应带有尾语“ Test”。
- 测试类中的Test Method应该带有注解@Test
#5楼
如果模块的包装未正确声明,则可能导致Maven找不到测试的另一件事。
在最近的情况下,有人使用<packaging>pom</packaging>
进行了测试,而我的测试从未运行过。 我将其更改为<packaging>jar</packaging>
,现在工作正常。
#6楼
如果您为测试添加“摘要”前缀,则会发现默认情况下也会将其忽略。
#7楼
如果项目具有<packaging>pom</packaging>
则Maven将不会运行测试
您需要将包装设置为jar(或其他Java artefact类型)才能运行测试: <packaging>jar</packaging>
#8楼
如果使用的JUnit不是标准( junit:junit
),例如,那么junitArtifactName
也可能是这种情况。
<dependency>
<groupId>org.eclipse.orbit</groupId>
<artifactId>org.junit</artifactId>
<version>4.11.0</version>
<type>bundle</type>
<scope>test</scope>
</dependency>
#9楼
在探索发现testng依赖性导致此问题之后,我也遇到了类似的问题。 从pom删除了testng依赖项之后(因为我不再需要它了),它对我来说开始正常工作。
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
#10楼
不运行测试用例的另一个原因发生在我身上-我有一个名为“ test”的属性,目的完全不同,但是它干扰了surefire插件。 因此,请检查您的POM:
<properties>
<test>.... </test>
...
</properties>
并删除它。
#11楼
/my_program/src/test/java/ClassUnderTestTests.java
应该
/my_program/src/test/java/ClassUnderTestTest.java
Maven查找Test的那些结尾或以Test开头以自动运行。
但是,您可以使用
mvn surefire:test -Dtest=ClassUnderTestTests.java
运行测试。
#12楼
万一有人搜索了但我没有解决,我有一个用于不同测试的库:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${org.junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
当我安装junit时,一切正常,我希望并提供帮助:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
#13楼
更新:
就像@scottyseus在评论中说的那样,从Maven Surefire 2.22.0开始,以下内容就足够了:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
使用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>
资料来源: https : //junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
#14楼
如果您的测试类名称未遵循标准命名约定(如上述@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>
...
#15楼
还有一个提示(除了以前的答案):
在Eclipse中,转到项目的“属性”>单击Run/Debug Settings
:
“此页面允许您使用当前选定的资源来管理启动配置”
您可以在其中添加(新建...)或删除(删除)项目中的任何JU(JUnit)测试(在src/test/java
文件夹或课程下)。
#16楼
这是我必须添加到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>
这是我的依赖项:
<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>
#17楼
如果您已在JUnit 4中编写了测试并将JUnit 5依赖项添加到surefire插件中,则测试将无法运行。
在这种情况下,只需注释来自surefire插件的JUnit 5依赖项:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<!--<dependencies>-->
<!--<dependency>-->
<!--<groupId>org.junit.platform</groupId>-->
<!--<artifactId>junit-platform-surefire-provider</artifactId>-->
<!--<version>1.0.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.junit.jupiter</groupId>-->
<!--<artifactId>junit-jupiter-engine</artifactId>-->
<!--<version>${junit.version}</version>-->
<!--</dependency>-->
<!--</dependencies>-->
</plugin>
#18楼
在我的情况下,它添加了junit-vintage-engine,使其与旧版本的JUnit测试兼容并可以运行它们。 当我使用JUnit 5时。
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
#19楼
我曾经使用此代码
<sourceDirectory>src_controller</sourceDirectory>
<testSourceDirectory>src_test</testSourceDirectory>
到我的pom.xml,只需确保特定的testng文件存在
<suiteXmlFile>/Users/mac/xxx/xxx/xx.xxxx.xx/xxx.restassured.xx/testng.xml</suiteXmlFile>
#20楼
我遇到了同样的问题,它通过以下pom.xml中的更改解决了:
<build>
<testSourceDirectory>test</testSourceDirectory>
...
变成:
<build>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
#21楼
过去,这些答案中的许多对我很有用,但是我想添加一个额外的方案,该方案花费了我一些时间,因为将来可能会对其他人有所帮助:
确保测试类和方法是公共的。
我的问题是我在使用IDE(IntelliJ)的自动测试类/方法生成功能,由于某种原因,它将它们创建为程序包专用。 我发现这比人们期望的更容易错过。
#22楼
当您将surfire插件3.x.x +与JUnit5一起使用,并错误地使用来自JUnit4的@Test
注释对测试类进行注释时,可能会发生此问题。
使用: org.junit.jupiter.api.Test
(JUnit5)代替org.junit.Test
(Junit4)
注意:这可能很难注意到,因为IDE可能会像JUnit4测试一样运行此问题。
#23楼
在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>
<!-- ... -->
#24楼
另一个容易忽视的问题-确保您的类文件具有.java扩展名
#25楼
就我而言,我们是将多模块应用程序迁移到Spring Boot。 不幸的是,maven不再在模块中执行所有测试。 测试类的命名没有改变,我们遵循命名约定。
最后,当我将依赖surefire-junit47
添加到插件maven-surefire-plugin
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>
#26楼
我为这个问题而苦苦挣扎。 就我而言,我没有导入正确的@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() {
}
}
#27楼
默认情况下,Maven在寻找要运行的测试时使用以下命名约定:
-
Test*
-
*Test
-
*TestCase
您的测试课程没有遵循这些约定。 您应该重命名它或将Maven Surefire插件配置为对测试类使用其他模式。