Maven does not find JUnit tests to run
我有一个Maven程序,它编译得很好。当我运行
我用一个非常简单的设置重新创建了这个问题,这个设置包括下面的内容,以及在使用
单元测试在Eclipse中运行良好(包括它的默认junit包,以及当我包含maven下载的junit.jar时)。另外,mvn
目录结构如下:
1 2 3 | /my_program/pom.xml /my_program/src/main/java/ClassUnderTest.java /my_program/src/test/java/ClassUnderTestTests.java |
POM.XML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?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> my_program</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>My Program</name> <dependencies> <dependency> <groupId>junit</groupId> junit</artifactId> <version>4.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project> |
ClassUnderest.java语言:
1 2 3 4 5 6 7 | public class ClassUnderTest { public int functionUnderTest(int n) { return n; } } |
classundertesttests.java语言:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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测试结束:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | [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] ------------------------------------------------------------------------ |
默认情况下,Maven在查找要运行的测试时使用以下命名约定:
Test* *Test *TestCase
您的测试类不遵循这些约定。您应该重命名它,或者配置maven-surefire插件为测试类使用另一个模式。
我还发现单元测试代码应该放在测试文件夹下,如果把它放在主文件夹下,它就不能被识别为测试类。如。
错误的
1 | /my_program/src/main/java/NotTest.java |
赖特
1 | /my_program/src/test/java/MyTest.java |
如果模块的包装声明不正确,另一件事可能会导致Maven找不到测试。
在最近的一个案例中,有人使用了
更新:
正如@scottyseus在评论中所说,从Maven Surefire 2.22.0开始,以下就足够了:
1 2 3 4 5 | <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> |
在使用JUnit5时,我遇到了同样的问题。Maven Surefire需要一个插件来运行JUnit 5测试。将此添加到我们的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.21.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>1.2.0-M1</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.2.0-M1</version> </dependency> </dependencies> </plugin> |
来源:https://junit.org/junit5/docs/current/user-guide/运行测试构建maven
此外,检查您的测试类目录(例如SRC/Test/Java)是否对应于在EDOCX1×4属性中的POM.XML中的属性EDOCX1 3中列出的目录。我花了一段时间才找到那个。
检查(对于JUnit-4.12和Eclipse Surefire插件)
如果项目有
您需要将包装设置为JAR(或其他一些Java人工制品类型),以便运行测试:EDCOX1×6
如果在测试前面加上"abstract",默认情况下也会忽略它。
如果你有一个共享的Java/Groovy应用程序,所有的都是Groovy单元测试,那么Maven就不会找到任何测试。这可以通过在SRC/Test/JAVA下添加一个单元测试来固定。
我也有类似的问题,在研究发现testng依赖性导致了这个问题之后。在从pom中删除了testng依赖项之后(因为我不再需要它),它开始对我起作用了。
1 2 3 4 5 6 | <dependency> <groupId>org.testng</groupId> testng</artifactId> <version>6.8</version> <scope>test</scope> </dependency> |
这些答案中的许多在过去对我非常有用,但我想添加一个额外的场景,这让我花费了一些时间,因为它可能在将来帮助其他人:
确保测试类和方法是公共的。
我的问题是我使用的是我的IDE(Intellij)的自动测试类/方法生成功能,出于某种原因,它将它们创建为包私有。我发现这比人们想象的要容易错过。
1 | /my_program/src/test/java/ClassUnderTestTests.java |
应该是
1 | /my_program/src/test/java/ClassUnderTestTest.java |
Maven会找到这些结束测试或从测试开始自动运行。
但是,您可以使用
1 | mvn surefire:test -Dtest=ClassUnderTestTests.java |
运行测试。
如果测试类名不遵循标准命名约定(如上面@axtavt所强调的那样),则需要在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <configuration> <includes> <include>**/*_UT.java</include> </includes> </configuration> </plugin> </plugins> </build> ... |
另一个不运行测试用例的原因发生在我身上——我有一个名为"test"的属性,用于完全不同的目的,但它干扰了Surefire插件。因此,请检查您的POM:
1 2 3 4 | <properties> <test>.... </test> ... </properties> |
把它拿走。
还有一个提示(除了前面的答案之外):
在Eclipse中,转到项目的属性>单击
"This page allows you to manage launch configurations with the
currently selected resource"
在这里,您可以添加(新的…)或删除(删除)您在项目中的任何JUnit(JUnit)测试(在
如果您已经在JUnit4中编写了测试并向Surefire插件添加了JUnit 5依赖项,那么您的测试将不会运行。
在这种情况下,只需注释来自SureFire插件的JUnit 5依赖项:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.19.1</version> <!--<dependencies>--> <!--<dependency>--> <!--<groupId>org.junit.platform</groupId>--> <!--junit-platform-surefire-provider</artifactId>--> <!--<version>1.0.0</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.junit.jupiter</groupId>--> <!--junit-jupiter-engine</artifactId>--> <!--<version>${junit.version}</version>--> <!--</dependency>--> <!--</dependencies>--> </plugin> |
在我的例子中,它添加了JUnit Vintage引擎,使其与旧版本的JUnit测试兼容,并可以运行它们。当我使用JUnit 5时。
1 2 3 4 5 | <dependency> <groupId>org.junit.vintage</groupId> junit-vintage-engine</artifactId> <scope>test</scope> </dependency> |
我也面临同样的问题,它通过在pom.xml中进行以下更改来解决:
1 2 | <build> <testSourceDirectory>test</testSourceDirectory> |
…
改为:
1 2 | <build> <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory> |
6月5日,我的工作很顺利
https://junit.org/junit5/docs/current/user-guide/运行测试构建maven
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <build> <plugins> <plugin> maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> <plugin> maven-failsafe-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> <!-- ... --> <dependencies> <!-- ... --> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-api</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency> <!-- ... --> </dependencies> <!-- ... --> |
如果使用的JUnit不是标准(
1 2 3 4 5 6 7 | <dependency> <groupId>org.eclipse.orbit</groupId> org.junit</artifactId> <version>4.11.0</version> <type>bundle</type> <scope>test</scope> </dependency> |
以下是我必须添加到pom.xml中的确切代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> maven-surefire-plugin</artifactId> <version>2.21.0</version> <dependencies> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>1.2.0-M1</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.2.0</version> </dependency> </dependencies> </plugin> </plugins> </build> |
以下是我的依赖关系:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-api</artifactId> <version>5.2.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.easytesting</groupId> fest-assert-core</artifactId> <version>2.0M10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> junit-platform-surefire-provider</artifactId> <version>1.2.0-M1</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-engine</artifactId> <version>5.2.0-M1</version> </dependency> </dependencies> |
我用过这个代码
1 2 | <sourceDirectory>src_controller</sourceDirectory> <testSourceDirectory>src_test</testSourceDirectory> |
对于我的pom.xml,只需确保其中特定的testng文件
1 | <suiteXmlFile>/Users/mac/xxx/xxx/xx.xxxx.xx/xxx.restassured.xx/testng.xml</suiteXmlFile> |
当您在JUnit5中使用Surface插件3.x.x+并错误地使用JUnit4中的
用途:用
注意:这可能很难被注意到,因为IDE可能会像JUnit4测试一样运行这个问题。
如果有人搜索过,而我没有解决,我有一个不同测试的库:
1 2 3 4 5 6 | <dependency> <groupId>org.junit.jupiter</groupId> junit-jupiter-api</artifactId> <version>${org.junit.jupiter.version}</version> <scope>test</scope> </dependency> |
当我安装JUnit时,一切正常,我希望并帮助:
1 2 3 4 5 6 | <dependency> <groupId>junit</groupId> junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> |