Maven使用cobertura-maven-plugin单元测试–测试覆盖率(加减乘除的测试)
遇到的问题:
Failed to execute goal org.codehaus.mojo:cobertura-maven-plugin:2.5.1:instrument (default) on project MavenTest: Execution default of goal org.codehaus.mojo:cobertura-maven-plugin:2.5.1:instrument failed: A required class was missing while executing org.c
解决办法:删maven仓库重新下载那些东西。cmd下搞
1.开始:
使用cobertura-maven-plugin
主要命令 test、cobertura:cobertura
配置.xml
<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.sise</groupId>
<artifactId>MavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MavenTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.doxia</groupId>
<artifactId>doxia-site-renderer</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.19.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>cobertura</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
主类代码块
package com.sise;
public class Caculate {
public int plus(int a, int b) {
return a + b;
}
public int minus(int a, int b) {
return a - b;
}
public int multipy(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
return a / b;
}
public int square(int a) { return a*a; }
}
测试代码块
package com.sise;
import org.junit.Assert;
import org.junit.Test;
public class CaculateTest {
@Test
public void plusTest() {
Caculate caculate = new Caculate();
Assert.assertEquals(10, caculate.plus(5, 5));
}
@Test
public void minusTest() {
Caculate caculate = new Caculate();
Assert.assertEquals(10, caculate.minus(15, 5));
}
@Test
public void multipyTest() {
Caculate caculate = new Caculate();
Assert.assertEquals(10, caculate.multipy(5, 2));
}
@Test
public void divideTest() {
Caculate caculate = new Caculate();
Assert.assertEquals(10, caculate.divide(50, 5));
}
@Test public void squareTest() {
Caculate caculate = new Caculate();
Assert.assertEquals(100, caculate.square(10)); }
}