Junit5 + Jacoco Maven配置

8 篇文章 0 订阅
5 篇文章 0 订阅

Junit依赖参照

官网
中文
依赖关系

maven配置- Jacoco offline-编译时插桩

<?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.example</groupId>
	<artifactId>junit5-maven-consumer</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<junit.version>4.12</junit.version>
		<junit.jupiter.version>5.0.2</junit.jupiter.version>
		<junit.vintage.version>${junit.version}.2</junit.vintage.version>
		<junit.platform.version>1.0.2</junit.platform.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.19.1</version>
				<configuration>
					<systemPropertyVariables>
						<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
					</systemPropertyVariables>
					<includes>
						<include>**/Test*.java</include>
						<include>**/*Test.java</include>
						<include>**/*Tests.java</include>
						<include>**/*TestCase.java</include>
					</includes>
					<properties>
						<!-- <includeTags>fast</includeTags> -->
						<excludeTags>slow</excludeTags>
						<!--
						<configurationParameters>
							junit.jupiter.conditions.deactivate = *
						</configurationParameters>
						-->
					</properties>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.junit.platform</groupId>
						<artifactId>junit-platform-surefire-provider</artifactId>
						<version>${junit.platform.version}</version>
					</dependency>
				</dependencies>
			</plugin>
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.8.6-SNAPSHOT</version>
				<executions>
					<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>default-report</id>
						<goals>
							<goal>report</goal>
						</goals>
					</execution>
					<execution>
						<id>default-check</id>
						<goals>
							<goal>check</goal>
						</goals>
						<configuration>
							<rules>
								<rule>
									<element>BUNDLE</element>
									<limits>
										<limit>
											<counter>COMPLEXITY</counter>
											<value>COVEREDRATIO</value>
											<minimum>0.60</minimum>
										</limit>
									</limits>
								</rule>
							</rules>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<!-- required: edit test and extention -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- required: run junit 5 tests -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
<!--		&lt;!&ndash; optional: junit configure api, used by IDE or build tools &ndash;&gt;-->
<!--		<dependency>-->
<!--			<groupId>org.junit.platform</groupId>-->
<!--			<artifactId>junit-platform-launcher</artifactId>-->
<!--			<version>${junit.platform.version}</version>-->
<!--			<scope>test</scope>-->
<!--		</dependency>-->
		<!-- optional junit 3/4 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- optional run junit 3/4 -->
		<!-- if there is junit 3/4 tests, this is required when run -->
		<dependency>
			<groupId>org.junit.vintage</groupId>
			<artifactId>junit-vintage-engine</artifactId>
			<version>${junit.vintage.version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<!--  must be on the classpath  -->
			<groupId>org.jacoco</groupId>
			<artifactId>org.jacoco.agent</artifactId>
			<classifier>runtime</classifier>
			<version>0.8.6-SNAPSHOT</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

</project>

maven配置- Jacoco one-the-fly - 运行时插桩

<?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.example</groupId>
	<artifactId>junit5-maven-consumer</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
		<junit.version>4.12</junit.version>
		<junit.jupiter.version>5.0.2</junit.jupiter.version>
		<junit.vintage.version>${junit.version}.2</junit.vintage.version>
		<junit.platform.version>1.0.2</junit.platform.version>
	</properties>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.7.0</version>
				<configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.19.1</version>
				<configuration>
					<includes>
						<include>**/Test*.java</include>
						<include>**/*Test.java</include>
						<include>**/*Tests.java</include>
						<include>**/*TestCase.java</include>
					</includes>
					<properties>
						<!-- <includeTags>fast</includeTags> -->
						<excludeTags>slow</excludeTags>
						<!--
						<configurationParameters>
							junit.jupiter.conditions.deactivate = *
						</configurationParameters>
						-->
					</properties>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.junit.platform</groupId>
						<artifactId>junit-platform-surefire-provider</artifactId>
						<version>${junit.platform.version}</version>
					</dependency>
				</dependencies>
			</plugin>
			<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.6-SNAPSHOT</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
		</plugins>
	</build>

	<dependencies>
		<!-- required: edit test and extention -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- required: run junit 5 tests -->
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>${junit.jupiter.version}</version>
			<scope>test</scope>
		</dependency>
<!--		&lt;!&ndash; optional: junit configure api, used by IDE or build tools &ndash;&gt;-->
<!--		<dependency>-->
<!--			<groupId>org.junit.platform</groupId>-->
<!--			<artifactId>junit-platform-launcher</artifactId>-->
<!--			<version>${junit.platform.version}</version>-->
<!--			<scope>test</scope>-->
<!--		</dependency>-->
		<!-- optional junit 3/4 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- optional run junit 3/4 -->
		<!-- if there is junit 3/4 tests, this is required when run -->
		<dependency>
			<groupId>org.junit.vintage</groupId>
			<artifactId>junit-vintage-engine</artifactId>
			<version>${junit.vintage.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

更多配置

Junit5 Example
Jacoco Example

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JUnitJaCoCoJava开发中常用的测试框架和代码覆盖率工具。其中JUnit是一个Java语言的单元测试框架,它提供了一些注解和断言方法,可以方便地编写和运行单元测试。而JaCoCo是一个Java代码覆盖率工具,可以帮助我们分析代码的测试覆盖率,从而找出测试不足的地方。 下面是一个使用JUnitJaCoCo进行单元测试和代码覆盖率分析的例子: ```java import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { @Mock private UserMapper userMapper; @InjectMocks private UserService userService; @Test public void testGetUserById() { UserVO user = new UserVO(); user.setId("1"); user.setName("张三"); when(userMapper.selectUserById("1")).thenReturn(user); UserVO result = userService.getUserById("1"); assertEquals("张三", result.getName()); } } ``` 上面的代码演示了如何使用JUnit和Mockito进行单元测试。其中,@RunWith注解表示使用MockitoJUnitRunner来运行测试,@Mock注解表示创建一个模拟的UserMapper对象,@InjectMocks注解表示将模拟的UserMapper对象注入到UserService对象中。在testGetUserById()方法中,我们使用Mockito.when()方法来定义当调用userMapper.selectUserById()方法时返回自定义的UserVO对象。最后,我们使用assertEquals()方法来判断测试结果是否符合预期。 接下来,我们使用JaCoCo来分析上面的代码的测试覆盖率。我们可以在pom.xml文件中添加以下配置: ```xml <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 然后,在命令行中执行以下命令: ``` mvn clean test ``` 执行完毕后,我们可以在target/site/jacoco/index.html文件中查看代码覆盖率报告。报告中会显示每个类和方法的覆盖率情况,以及哪些代码没有被覆盖到。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值