利用maven结合Junit4与cobertura进行单元测试

问题说明:

本人利用maven结合Junit与cobertura插件进行测试时,直接执行如下命令:

mvn clean test cobertura:cobertura

结果完全正常(pom文件中未声明cobertura插件的版本、配置等信息),用例覆盖率也正常达到了94%。pom配置如下:

<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>edu.zju.cst.w3</groupId>
	<artifactId>AootHomework</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.3</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<!-- 指定maven编译的jdk版本 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
		</plugins>
		
	</build>
</project>

但是,当我在pom文件中指定cobertura插件及maven-surefire-plugin插件时,则无法进行该概率测试了。

问题点:在使用cobertura插件进行覆盖率测试时所有测试用例全部未通过

解决:出现该问点的原因是在项目中字节码处理工具(编译器)不兼容的缘故,即jdk1.8之前可以使用“-XX:-UseSplitVerifier”来解决这个问题,但是jdk1.8版本之后删除了该属性,需要用“-noverify”来配置。参考下图:
stackoverflow截图
stackoverflow截图
stackoverflow截图

最后提供一个作者整理的一个maven用于单元测试的pom模板

模板中也指定了编译及测试过程中常用的一些插件,希望对大家有帮助!

<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>edu.zju.cst.w3</groupId>
	<artifactId>AootHomework</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.3</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<!-- 指定maven编译的jdk版本 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
			</plugin>
			<!-- maven里执行测试用例的插件,若声明该项则maven执行mvn test时加载默认版本 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<argLine>-noverify -XX:-UseSplitVerifier</argLine>
				</configuration>
			</plugin>
			<!-- 项目API doc报告 命令mvn javadoc:javadoc -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>2.7</version>
				<!-- 多模块maven项目时执行一下命令,在父项目执行命令,则在父类target生成doc文件 -->
				<configuration>
					<!-- <aggregate>true</aggregate> -->
					<encoding>UTF-8</encoding>
					<charset>UTF-8</charset>
					<tags>
						<tag>
							<placement>a</placement>
						</tag>
					</tags>
				</configuration>
			</plugin>
			<!-- 单元测试报告html,单需要单独执行名mvn surefire-report:report -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-report-plugin</artifactId>
				<version>2.12.2</version>
				<configuration>
					<showSuccess>false</showSuccess>
				</configuration>
			</plugin>
			<!-- 测试覆盖率的报告 ,命令 mvn cobertura:cobertura -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>cobertura-maven-plugin</artifactId>
				<version>2.5.1</version>
				<configuration>
					<formats>
						<format>html</format>
						<format>xml</format>
					</formats>
				</configuration>
				<executions>
					<execution>
						<id>cobertura-report</id>
						<goals>
							<goal>cobertura</goal>
						</goals>
						<phase>test</phase>
					</execution>
				</executions>
			</plugin>
		</plugins>
		
	</build>
</project>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值