Maven没有findJUnittesting运行

我有一个maven程序,编译得很好。 当我运行mvn test它不会运行任何testing(在TESTs头下说There are no tests to run. )。

我已经用一个超级简单的设置重新创build了这个问题,我将在下面包括以及用-X运行时的输出。

unit testing在eclipse中运行良好(包括默认的junit包,以及包含由maven下载的junit.jar)。 另外mvn test-compile正确地创build了testing类下的类。 我正在使用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 -Xtesting结束:

 [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 ------------------------------------------------------- TESTS ------------------------------------------------------- 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在查找要运行的testing时使用以下命名约定:

  • Test*
  • *Test
  • *TestCase

你的testing类不遵循这些约定。 您应该重新命名它或者configurationMaven Surefire插件来为testing类使用另一种模式。

我还发现unit testing代码应放在testing文件夹下,如果放在主文件夹下面,则不能将其识别为testing类。 例如。

错误

 /my_program/src/main/java/NotTest.java 

 /my_program/src/test/java/MyTest.java 

另外一件事情,如果模块的包装没有被正确的声明,可能会导致Maven找不到testing。

在最近的情况下,有人有<packaging>pom</packaging> ,我的testing从未跑。 我把它改成<packaging>jar</packaging> ,现在工作正常。

此外,请检查您的testing类目录(例如src / test / java)是否对应于<build>属性下的pom.xml中属性<testSourceDirectory>中列出的目录。 花了我一段时间才发现。

检查(对于jUnit – 4.12和Eclipse的surefire插件)

  1. 在依赖项中添加POM.xml中所需的jUnit版本。 做Maven – >更新项目,以查看在项目中导出所需的jar子。
  2. testing类位于文件夹src / test / java和该文件夹的子目录下(或者可以在configurationtestSourceDirectory中的POM中指定基础文件夹)。 class级名称应该有“Test”字样。
  3. testing类中的testing方法应该有@Test注释

如果你有一个共享的Java / Groovy应用程序,并且你所有的都是Groovyunit testing,那么Maven将不会find任何testing。 这可以通过在src / test / java下添加一个unit testing来解决。

如果您使用“抽象”前缀进行testing,则会被默认忽略。

如果项目有<packaging>pom</packaging> Maven将不会运行您的testing

您需要将包装设置为jar(或其他一些java artefacttypes)以供testing运行: <packaging>jar</packaging>

我也有类似的问题,经过探索发现testng的依赖是造成这个问题。 从pom中删除testng依赖项(因为我不再需要它),它开始为我工作得很好。

  <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> <scope>test</scope> </dependency> 

不运行testing用例的另一个原因发生在我身上 – 为了完全不同的目的,我有一个名为“test”的属性,但是它干扰了surefire插件。 因此,请检查您的POM:

 <properties> <test>.... </test> ... </properties> 

并删除它。

junitArtifactName也可能是如果使用的JUnit不是标准( junit:junit ),但是例如…

 <dependency> <groupId>org.eclipse.orbit</groupId> <artifactId>org.junit</artifactId> <version>4.11.0</version> <type>bundle</type> <scope>test</scope> </dependency>