SpringBoot配置Jacoco生成测试覆盖率报告(包括Powermock、Mockito用例)
程序员文章站
2024-03-15 19:38:18
...
最近项目中由于与前端在联调过程中发现接口不通,导致联调阶段浪费很多时间,造成接口不通的主要原因是代码逻辑不通,所以我向项目组建议引入Jacoco工具来对工程代码生成测试覆盖率报告,这样就不会出现“前端问接口到底测试没测试,后端说我测试了啊”的这种情况了,特此记录便于日后查阅。
1、maven依赖
首先在项目中引入maven依赖,以下的依赖放到项目的根pom.xml就可以了,代码如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.0.3.RELEASE</version>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${basedir}/target/jacoco.exec</dataFile>
<outputDirectory>${basedir}/target/site/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<!--suppress UnresolvedMavenProperty -->
<argLine>${surefireArgLine}</argLine>
<skipTests>false</skipTests>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
2、测试用例
我们需要编写测试覆盖率用例来生成报告,这里有一个非常重要的地方要注意,我的用例存放的结构必须是在/test/java/目录下,目录结构如下:
代码如下:
package com.openailab.oascloud.file.service;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.openailab.oascloud.common.model.dfm.ResourceBO;
import com.openailab.oascloud.common.model.dfm.ResourceStatisticBO;
import com.openailab.oascloud.common.model.dfm.vo.ResourceVO;
import com.openailab.oascloud.file.common.config.BootstrapConfig;
import com.openailab.oascloud.file.dao.FileDao;
import com.openailab.oascloud.file.service.impl.FileServiceImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
/**
* @description: 数据文件管理-service
* @author: zhangzhixiang
* @createDate: 2019/12/9
* @version: 1.0
*/
@RunWith(SpringRunner.class)
public class FileServiceImplTest {
@Mock
private FileDao fileDao;
@Mock
private BootstrapConfig bootstrapConfig;
@InjectMocks
private FileServiceImpl fileService;
@Before
public void setUp() {
when(fileDao.queryResourceList(any(ResourceBO.class))).thenReturn(getResourcePageBO());
when(bootstrapConfig.getFileRoot()).thenReturn("/usr/local/oas");
}
@Test
public void queryResourceByCondition() {
//case01
ResourceVO resourceVO = new ResourceVO();
resourceVO.setResourceId(1);
List<ResourceBO> resourceBOList1 = fileService.queryResourceByCondition(resourceVO);
Assert.assertEquals(Integer.valueOf(1), resourceBOList1.get(0).getId());
//case02
// List<ResourceBO> resourceBOList2 = fileService.queryResourceByCondition(any(ResourceVO.class));
// Assert.assertEquals(Integer.valueOf(1), resourceBOList2.get(0).getId());
}
@Test
public void resourceStatistic() throws Exception {
//case01
String parentDir = "/file";
List<ResourceStatisticBO> resourceStatisticBOList = fileService.resourceStatistic(parentDir);
boolean ret = Optional.ofNullable(resourceStatisticBOList).isPresent();
Assert.assertEquals(true, ret);
}
@Test
public void replaceFileContent() {
//case01
Boolean ret2 = fileService.replaceFileContent(getResourceVO());
Assert.assertEquals(true, ret2);
}
/**
* 伪造resroucePageBO
*
* @param
* @return com.github.pagehelper.PageInfo<com.openailab.oascloud.common.model.dfm.ResourceBO>
* @author zxzhang
* @date 2020/4/14
*/
private PageInfo<ResourceBO> getResourcePageBO() {
PageInfo<ResourceBO> pageInfo = new PageInfo<>();
List<ResourceBO> resourceBOList = Lists.newArrayList();
ResourceBO resourceBO1 = new ResourceBO();
resourceBO1.setId(1);
resourceBO1.setFileName("xxx");
resourceBOList.add(resourceBO1);
ResourceBO resourceBO2 = new ResourceBO();
resourceBO2.setId(2);
resourceBO2.setFileName("mmm");
resourceBOList.add(resourceBO2);
pageInfo.setList(resourceBOList);
return pageInfo;
}
/**
* 伪造resrouceVO
*
* @param
* @return com.openailab.oascloud.common.model.dfm.vo.ResourceVO
* @author zxzhang
* @date 2020/4/14
*/
private ResourceVO getResourceVO() {
ResourceVO resourceVO = new ResourceVO();
resourceVO.setResourceId(1);
resourceVO.setFileName("xxx");
return resourceVO;
}
}
3、生成报告
通过以下maven命令生成测试覆盖率报告,代码如下:
mvn clean test -Dmaven.test.failure.ignore=true
生成的报告目录结构如下:
打开 /target/site/jacoco/index.html 查看覆盖率报告,图示如下:
绿色的部分是测试用例覆盖到的代码,红色的部分是测试用例未覆盖到的代码。