首先需要有junit测试依赖包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
需要两个插件maven-surefire-plugin和maven-surefire-report-plugin
具体可以查看官方文档:https://maven.apache.org/plugins/index.html
在pom.xml文件中添加一下两个插件
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<!-- 2.19.1才开始支持指定方法的测试 -->
<version>2.19.1</version>
<!-- 跳过测试 -->
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<showSuccess>false</showSuccess>
</configuration>
</plugin>
</plugins>
</reporting>
测试方法:
跑指定的一个方法:
mvn surefire:test -Dtest=FacadeTradeService4FeeShareTest
跑指定的多个方法
mvn surefire:test -Dtest=FacadeTradeService4FeeShareTest,FacadeTradeServiceTest
字符匹配模式
mvn surefire:test -Dtest=FacadeTrade*Test
debug模式测试
mvn -Dmaven.surefire.debug test
仅仅报告失败测试
mvn surefire-report:report -DshowSuccess=false
在linux下将测试结果输出到指令文件(设置系统参数为oracle或mysql)
mvn clean test -D db=oracle > /usr/local/result/1.3.10.12.oracle.result.txt
mvn clean test -D db=mysql > /usr/local/result/1.3.10.12.mysql.result.txt
查看结果
在对应项目目录下target\surefire-reports可以查看测试结果
添加插件到 中
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<showSuccess>false</showSuccess>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</reporting>
mvn clean site -Dtest=QuotaLimitService4BondTest 指定测试
mvn clean site