本地电脑环境
window10
jdk1.8
maven 3.5.4
IDEA 2020.1
首先配置好maven环境
这里面有一些坑,可以看我的另一篇博客,踩过的坑希望大家不要在踩了。
https://blog.csdn.net/ibo123/article/details/108034402
无法加载主类的问题在于你的maven环境没有进行全局配置,请务必配置好全局maven。要不然IDEA会自动加载默认的maven。
1 构建一个简单的maven项目
file -> project -> Maven-> next
填写 Goupid Artifactid -> Next
项目名称和路径根据自己需要填写,最后点击Finish.
2 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibo.pit</groupId>
<artifactId>pit_test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- 不设置scope就是全局-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.4.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>1.4.11</version>
<configuration>
<targetClasses>
<param>com.ibo.pit.*</param>
</targetClasses>
<targetTests>
<param>com.ibo.test.*</param>
</targetTests>
<!-- <mutators>-->
<!-- <mutator>CONSTRUCTOR_CALLS</mutator>-->
<!-- <mutator>VOID_METHOD_CALLS</mutator>-->
<!-- <mutator>RETURN_VALS</mutator>-->
<!-- <mutator>NON_VOID_METHOD_CALLS</mutator>-->
<!-- </mutators>-->
</configuration>
<executions>
<execution>
<id>pit-report</id>
<!-- optional, this example attached the goal into mvn test phase -->
<phase>test</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3 在main->java和test->java下新建两个包,并在main创建java class。
calculate.java
public class calculate {
public int add(int a, int b) {
int result = a + b;
return result;
}
public int subtract(int a, int b) {
int result = a - b;
return result;
}
public int multiply(int a, int b) {
int result = a * b;
return result;
}
public int divide(int a, int b) {
int result = a / b;
return result;
}
public int summation(int start){
int retVal = 0;
while (start > 0 ){
retVal += start;
start--;
}
return retVal;
}
public boolean isPositive(int number){
return number > 0;
}
public int compare(int a, int b){
int retVal = 0;
if (a == b){
retVal = 0;
}else if (a >= b){
retVal = 1;
}else {
retVal = -1;
}
return retVal;
}
public static void main(String[] args){
calculate c = new calculate();
int a = c.add(1,2);
System.out.println(a);
}
}
4 对calculate类生成单元测试,使用junit。
public class calculateTest {
@Test
public void add() {
assertEquals(2,new calculate().add(1,1));
}
@Test
public void subtract() {
assertEquals(1,new calculate().subtract(2,1));
}
@Test
public void multiply() {
assertEquals(2,new calculate().multiply(1,2));
}
@Test
public void divide() {
assertEquals(2,new calculate().divide(4,2));
}
@Test
public void summation() {
assertEquals(3, new calculate().summation(2));
}
@Test
public void isPositive() {
assertEquals(true, new calculate().isPositive(2));
}
@Test
public void compare() {
assertEquals(-1, new calculate().compare(-1,0));
}
}
5 运行calculateTest,确保单元测试通过。
6 IDEA终端 mvn clean
7 mvn clean test
默认Pitest生成html报告,此报告在target/pit-reports里面找到。
可以根据此报告来修改代码和测试用例,达到100%代码覆盖,和较高的mutation coverage。
欢迎软工和变异领域的大佬合作交流,qq:1564271420.
来源:oschina
链接:https://my.oschina.net/u/4283198/blog/4503331