使用junit :直接在pom文件中直接引入:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<epps-pccs.version>1.0.0</epps-pccs.version>
<hibernate-validator.version>4.2.0.Final</hibernate-validator.version>
<mybatis.version>3.0.6</mybatis.version>
<mybatis-spring.version>1.0.2</mybatis-spring.version>
<dbcp.version>1.2.2</dbcp.version>
<cxf-version>2.6.1</cxf-version>
<xstream.version>1.4.3</xstream.version>
<junit.version>4.8.1</junit.version>
......
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
引入mock:
<tcc.version>0.0.5</tcc.version>
<epps-task.version>2.0.0-SNAPSHOT</epps-task.version>
<mockito-all.version>2.0.2-beta</mockito-all.version>
<sonar.coverage.exclusions>**/basis/*,**/cache/*,**/client/*,**/exception/*,**/general/*,**/rsf/*,**/scm/*,**/service/*,**/task/*,**/uts/*</sonar.coverage.exclusions>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito-all.version}</version>
<scope>test</scope>
</dependency>
然后引入jacoco:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
<configuration>
<skip>${jacoco.skip.instrument}</skip>
</configuration>
<executions><execution>
<id>pre-unit-test</id>
<goals><goal>prepare-agent</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal></goals>
</execution><execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal></goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal></goals>
<phase>prepare-package</phase><configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile></configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>${project.build.directory}/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<argLine>-noverify -XX:-UseSplitVerifier -XX:NewSize=128m -XX:MaxNewSize=512m -XX:PermSize=128m -XX:MaxPermSize=768m
${surefireArgLine}
</argLine></configuration>
</plugin>
</plugins>
对于junit 没有统计到覆盖率:
多出:
<dependency>
<!--<groupId>org.powermock</groupId>-->
<!--<artifactId>powermock-module-junit4</artifactId>-->
<!--<version>${powermock.version}</version>-->
<!--<scope>test</scope>-->
</dependency>
<dependency>
<!--<groupId>org.powermock</groupId>-->
<!--<artifactId>powermock-api-mockito</artifactId>-->
<!--<version>${powermock.version}</version>-->
<!--<scope>test</scope>-->
</dependency>
<dependency>
<!--<groupId>org.powermock</groupId>-->
<!--<artifactId>powermock-module-junit4-rule-agent</artifactId>-->
<!--<version>${powermock.module.rule.agent.version}</version>-->
<!--<scope>test</scope>-->
</dependency>
<dependency>
<!--<groupId>org.powermock</groupId>-->
<!--<artifactId>powermock-core</artifactId>-->
<!--<version>${powermock.version}</version>-->
<!--<scope>test</scope>-->
</dependency>
还有如果按模块单测,那么每一个模块都要单独引入:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
#####################################################################################################
正常被jacoco统计的junit +mock 单元代码:
import com.alibaba.fastjson.JSONObject;
import com.***.epp.pccs.common.enums.BusinessCodeEnum;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
/**
*
*/
public class LogWrapperTest {
@InjectMocks
private LogWrapper logWrapper;
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void buildMonitorLogString_test() {
JSONObject req = new JSONObject();
req.put("payAmount", "123.45");
LogWrapper.buildMonitorLogString(req, new JSONObject(), "1234567890", BusinessCodeEnum.DEPOSIT, "120");
}
}
***********************************************对于没有统计到的单测**********************************************************
pom文件里多出了testng插件:
<dependency>
<!--<groupId>org.testng</groupId>-->
<!--<artifactId>testng</artifactId>-->
<!--<version>6.4</version>-->
<!--<scope>test</scope>-->
</dependency>