SpringBoot通过Jacoco生成用例覆盖率报告(包括Mockito生成测试用例)

最近项目到了联调阶段,主管需要我通过Jacoco计算一下测试用例的覆盖率,并生成一份测试用例覆盖率报告。

1、引入依赖

<dependencies>
    <!-- test -->
    <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>  
</dependencies>

<build>
    <plugins>
        <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>
    </plugins>
</build>

上面的pom配置需要放到你需要生报告的pom.xml中,比如这里我配置到了ops-biz包下的pom.xml里面。

相关测试用例也必须放到ops-biz的test下面,这样才能在ops-biz包下生成对应的用例覆盖率报告

2、测试用例

/**
 * @Description:ClueInfoServiceTest
 * @Author:zhangzhixiang
 */
@RunWith(SpringRunner.class)
public class ClueInfoServiceTest {
    
    @Mock
    private ClueInfoDAO clueInfoDAO;
    
    @InjectMocks
    private ClueInfoService clueinfoservice = new ClueInfoServiceImpl();

    @Before
    public void init() {
        when(clueInfoDao.isExist(any(ClueInfoDO.class))).thenReturn(1L);
    }

    @Test
    public void isExist() {
        //case01
        ClueInfoBO clueInfoBO = new ClueInfoBO();
        Boolean flag = clueInfoService.isExist(clueInfoBO);
        Assert.assertEquals(true, flag);
        //case02
        when(clueInfoDao.isExist(any(ClueInfoDO.class))).thenReturn(null);
        Boolean flag = clueInfoService.isExist(clueInfoBO);
        Assert.assertEquals(false, flag);
        //case03
        when(clueInfoDao.isExist(any(ClueInfoDO.class))).thenReturn(-1L);
        Boolean flag = clueInfoService.isExist(clueInfoBO);
        Assert.assertEquals(false, flag);
    }
}

3、运行maven命令

    mvn clean verify -DfailIfNoTests=false

之后用例覆盖率报告就会在/ops-biz/target/site文件夹下面生成。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BasicLab基础架构实验室

你的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值