2017-03-01 84 views
2

我试图在测试类中运行单个测试,但类中的所有测试都已运行。尽管指定单个测试,但Maven仍在类中运行所有测试

我运行它像这样

mvn clean test -Dtest=TestClass#testMethod 

,我已经试过

mvn clean test -Dtest="TestClass#testMethod" 

mvn clean test "-Dtest=TestClass#testMethod" 

,但似乎没有任何工作。 TestClass中的所有测试都可以运行...

我在这里错过了什么?

路径测试:/development/src/test/java/com/company/project/TestClass.java

TestClass.java

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.junit4.SpringRunner; 

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class TestClass { 

    @Test 
    public void testMethod1() throws Exception { 
    System.out.println("running1"); 
    } 

    @Test 
    public void testMethod2() throws Exception { 
    System.out.println("running2"); 
    } 
} 

输出从mvn -Dtest=TestClass#testMethod1 clean test

[INFO] 
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ lazarus --- 

------------------------------------------------------- 
T E S T S 
------------------------------------------------------- 
Running TestClass 
SLF4J: Class path contains multiple SLF4J bindings. 

.. 

running1 
running2 
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.536 sec - in TestClass 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 50.452 s 
[INFO] Finished at: 2017-03-01T13:42:51-05:00 
[INFO] Final Memory: 41M/1781M 
[INFO] ------------------------------------------------------------------------ 

编辑2:电源模拟依赖是造成这个问题?

<dependency> 
     <groupId>org.powermock</groupId> 
     <artifactId>powermock-mockito-release-full</artifactId> 
     <version>1.6.4</version> 
     <classifier>full</classifier> 
     <scope>test</scope> 
    </dependency> 
+0

那么......你有'TestClass'类和'testMethod'方法吗?你可以在磁盘上显示它的完整路径吗? – Tunaki

+0

@Tunaki更新! – javajavajava

+0

您使用的是哪个版本的JUnit? – Tunaki

回答

3

这是一个棘手的错误。 powermock-mockito-release-full依赖项引入了类路径TestNG,并且您希望使用JUnit运行测试,因为您的测试方法使用JUnit org.junit.Test注释进行了注释。这是故障的原因。

当您不指定使用哪个提供程序运行测试时,the Surefire Plugin will try to be clever并为您检测。目的是如果你依赖于TestNG,它将自动选择它的TestNG提供者来运行你的测试。相反,如果您依赖于JUnit,它将使用其JUnit提供程序。这甚至是depends on the version of TestNG or JUnit。这对于简化配置非常有用,但是当两者都在类路径中时...未指定会发生什么。事实证明,Surefire 2.19.1将首先选择TestNG(参见源代码auto-detecting the providerTestNG being specified before any JUnit specific providers in the list of well known providers)。你可以在调试模式下查看日志情况(使用-X);当开始测试时,您会看到

Running com.company.project.TestClass 
Configuring TestNG with: TestNG60Configurator 

表明正在使用TestNG提供程序。

这里会有几个解决方案。首先,在测试类路径中同时使用TestNG和JUnit可能不是你想要的东西,除非你明确地想要为你的测试使用两个框架。因此,简约的解决办法是排除TestNG的全部为powermock-mockito-release-full传递依赖,所以它不会在测试classpath中结束了,而事实上,加入

<exclusions> 
    <exclusion> 
    <groupId>org.testng</groupId> 
    <artifactId>testng</artifactId> 
    </exclusion> 
</exclusions> 

解决这个问题,只有1测试用正确的-Dtest=TestClass#testMethod1调用运行。

也许更好的第二个解决方法是删除带来的其他事情上的测试类路径中,你最有可能不想很多powermock-mockito-release-full的依赖。值得注意的是,它甚至带来了源JAR!如果您只依赖于powermock-module-junit4,则问题也得到解决,而不会混乱您的类路径。您可以自行添加对Mockito或您需要的其他特定组件的依赖关系。

第三个解决方案是to force the use of the JUnit 47 provider。这将确保测试使用JUnit运行,而不是TestNG,即使两者都在测试类路径中。

+0

Woah ...感谢真棒的回答!非常感激! – javajavajava

0

你说的没错。只需将test移动到该行的末尾即可。

mvn -Dtest=TestClass#testMethod clean test 
+0

我希望它那么简单。那也行不通。这是一个春季启动应用程序,我使用@RunWith(SpringRunner.class)@SpringBootTest注释。这有什么区别吗? – javajavajava

相关问题