2011-05-30 42 views
284

我有一个Maven的程序,它编译罚款。当我运行mvn test它不会运行任何测试(在TESTs标题下说There are no tests to run.)。的Maven没有找到JUnit测试运行

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

单元测试从蚀(既使用其默认的junit包,当我代替包括由行家下载的junit.jar)运行正常。 mvn test-compile也正确地创建了测试类下的类。我在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 

的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; 
    } 

} 

ClassUnderTest Tests.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: [email protected]] 
[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 = [email protected] 
[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] ------------------------------------------------------------------------ 

回答

497

默认的Maven寻找时使用以下命名约定的测试运行:

  • Test*
  • *Test
  • *TestCase

你的测试类没有遵守这些约定。您应该重命名它或configure Maven Surefire Plugin以使用其他模式进行测试。

+1

这确实是发生了什么 - 我没有注意到我搞砸了命名约定。非常感谢你! – Zugwalt 2011-05-30 16:33:25

+41

我只是浪费了一个小时,试图找出我的POM文件出了什么问题。我不敢相信它和命名惯例一样简单。 – 2011-09-17 01:40:00

+8

我用了差不多30分钟的时间搞清楚了这一点。虽然我的IDE很高兴。 (即使我的IDE声称100%maven严格)。谢谢! :) – 2012-01-04 14:36:56

57

我还发现,单元测试代码应该测试文件夹下的说,它不能被识别为测试类,如果你把它的主文件夹下。 例如。

错误

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

/my_program/src/test/java/MyTest.java 
+1

Thaaank你!这和设置范围来测试(' test ')在'pom.xml'文件中为我做了。 – dinesharjani 2017-05-22 18:28:49

4

如果你有一个共享的Java/Groovy的应用程序,你必须是Groovy进行单元测试,然后Maven会找不到任何测试。这可以通过在src/test/java下添加一个单元测试来解决。

22

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

9

检查(针对JUnit - 4.12和Eclipse保命插件)

  1. 添加所需的JUnit版本中的pom.xml依赖关系。做Maven - >更新项目以查看项目中导出的必需jar文件。
  2. 测试类位于该文件夹的src/test/java和子目录下(或者可以在配置testSourceDirectory中的POM中指定基础文件夹)。班级名称应该有'Test'字样。
  3. 在测试类测试方法应该有注释@Test
+1

这更多的是java配置问题,但除了正确命名测试类并将测试文件放在src下的测试目录之外,测试类的包名称必须与您正在测试的类的包名称相匹配。 – Paul 2015-05-29 13:39:39

+2

@Paul False - Maven将执行[在src/test/java'下匹配约定的所有类](http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html) 。包约定用于结构并允许测试访问包私有方法。 – 2015-08-24 18:02:09

+0

我明白了,多谢@ michael – Paul 2015-08-25 11:53:46

35

,可能会导致Maven来找不到测试,如果如果模块的包装上未正确申报另一件事。

在最近的一个案例中,有人有<packaging>pom</packaging>,我的测试从未跑过。我将其更改为<packaging>jar</packaging>,现在它工作正常。

+1

非常好的建议!演示将单个模块Maven构件拆分为多个时,“复制粘贴”的危险性。 – morsor 2017-11-15 07:51:13

+0

它拯救了我的生命。 – user5698801 2017-12-26 11:01:39

7

如果您在“抽象”前加一个测试,它会被默认忽略。

+2

请在相关文件指向这个 – 2016-02-29 09:34:43

+1

https://maven.apache.org/guides/getting-started/ ''' 和默认排除的因素: ** /摘要* Test.java ** /摘要* TestCase.java ''' – 2017-11-29 15:52:37

+0

它似乎也忽略'*/TestAbstractSomeClassName.java'。 – 2018-03-01 06:06:33

6

Maven将不会运行测试,如果该项目有<packaging>pom</packaging>

您需要设置为测试封装到罐子(或某些其他Java的人工制品类型)来运行:<packaging>jar</packaging>

0

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> 
2

我也有类似的问题,探讨后发现,TestNG的依赖关系是CA使用这个问题。从pom中移除testng依赖项(因为我不再需要它),它开始为我工作得很好。

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

删除testng解决了我的问题,但为什么? – hogi 2017-11-04 11:21:27

1

的另一个原因是没有运行测试案例发生在我身上 - 我有一个名为“test”为完全不同的用途属性,但它与神火插件干扰。因此,请检查您的POM:

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

并将其删除。

1
/my_program/src/test/java/ClassUnderTestTests.java 

应该

/my_program/src/test/java/ClassUnderTestTest.java 

行家认定这些目标测试或测试开始自动运行。

但是,您可以使用

mvn surefire:test -Dtest=ClassUnderTestTests.java 

运行测试。