1. 程式人生 > >利用maven結合Junit4與cobertura進行單元測試

利用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>