运行测试类中指定的方法:(这个需要maven-surefire-plugin:2.7.3以上版本才能支持)
>mvn test -Dtest=[ClassName]#[MethodName]
//[MethodName]为要运行的方法名,支持*通配符,范例:
>mvn test -Dtest=MyClassTest#test1
>mvn test -Dtest=MyClassTest#*test*
仿照了spring源代码中的测试类的命名,全部都是以Tests结尾的,但是搬过来用后,运行mvn test,却报找不到任何测试类,如下:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
T E S T S
-------------------------------------------------------
There are no tests to run.
surefire插件执行测试的,它按照指定格式的类名来查找匹配的测试类,
默认包含的测试类:
- **/*Test.java
- **/Test*.java
- **/*TestCase.java
默认排除的测试类:
- **/Abstract*Test.java
- **/Abstract*TestCase.java
因此默认情况下“**/*Tests.java”是不会被mvn test发现并执行的,可按如下修改surefire插件的配置以达到包含"**/*Tests.java"测试类的目的:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
<excludes>
<exclude>**/Abstract*.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Ref:
http://www.oschina.net/code/snippet_157514_37277
http://rongjih.blog.163.com/blog/static/335744612010102911363452/
© 著作权归作者所有