SpringBoot 单元测试(cobertura 生成覆盖率报

2019-05-31  本文已影响0人  PC_Repair

demo(SpringBoot 项目)

被测试类:

import org.springframework.stereotype.Service;

@Service
public class TestService {
    public String sayHi() {
        return "hi";
    }

    public int divide(int a, int b) {
        return a / b;
    }
}

测试代码:

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestServiceTest {
    @Autowired
    TestService testService;

    @Test
    public void testSayHi() {
        TestService testService = new TestService();
        String result = testService.sayHi();
        assertEquals("hi", result);
    }

    @Test
    public void testDivide() {
        TestService testService = new TestService();
        int result = testService.divide(3, 6);
        assertTrue(result > -1);
    }
}

pom.xml 配置文件

![cobertura](C:\Users\jiaflu\Desktop\cobertura.PNG)<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jiaflu</groupId>
    <artifactId>learn_springoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>learn_springoot</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <jackson.version>2.9.8</jackson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <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.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
                </configuration>
            </plugin>

        </plugins>
    </build>
</project>

运行mvn cobertura:cobertura 查看截图:

cobertura.PNG

覆盖率测试报告生成(cobertura)

cobertura 原理

cobertura执行过程大致如下:
1,使用instrument修改我们编译后的class文件,位于 target\generated-classes
2,执行测试,测试数据输出到xxx.ser中,位于 target\cobertura\cobertura.ser
3,使用report生成覆盖率报告。

1. instrument

使用 instrument 执行的过程中,CoberturaInstrumenter 会首先调用分析监听器分析给定的编译好的.class,获得touchPoint(可以认为对应于源代码中的待覆盖行)以及需要的其他信息。然后调用注入监听器将信息注入到新的.class中,保存到 \target\generated-classes 目录下。

示例:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.cisco.webex.cmse.soa.soaservice.service;

import net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;
import net.sourceforge.cobertura.coveragedata.TouchCollector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

@PropertySource({"classpath:application.properties"})
@Service
public class PropertyService implements HasBeenInstrumented {
    private static final Logger logger;
    @Value("${cdp.instance.url}")
    private String cdpInstanUrl;
    @Value("${soa.instance.url}")
    private String soaInstanceUrl;
    @Value("${github.api.token}")
    public String gitApiToken;
    @Value("${github.instance.url}")
    private String githubInstance;
    @Value("${github.repo.template.owner}")
    private String tplRepoOwner;
    @Value("${github.repo.consul.owner}")
    private String consulRepoOwner;
    @Value("${slm.listen.queue.name}")
    private String slmListenQueue;

    public PropertyService() {
        boolean var1 = false;
        int __cobertura__branch__number__ = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 12);
        super();
    }

    public String getCdpInstanUrl() {
        boolean var1 = false;
        int __cobertura__branch__number__ = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 33);
        return this.cdpInstanUrl;
    }

    ...

    public void setSlmListenQueue(String ()V) {
        boolean var2 = false;
        int __cobertura__branch__number__ = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 85);
        this.slmListenQueue = slmListenQueue;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 86);
    }

    static {
        boolean var0 = false;
        boolean var1 = true;
        TouchCollector.touch("com.cisco.webex.cmse.soa.soaservice.service.PropertyService", 13);
        logger = LoggerFactory.getLogger(PropertyService.class);
    }
}

2. 执行测试

在执行测试用例时,引用 cobertura 修改过的.class,测试信息写入到cobertura.ser档案文件。

3.生成报告

从cobertura.ser获取覆盖率数据,然后结合src中提供的源代码,生成最终的覆盖率报告,放到了target\site\cobertura路径下。若配置了生成 html 格式的报告,可以通过 index.html 查看覆盖率测试报告。

SpringBoot pom.xml 配置

添加如下依赖:

        <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <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.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <!-- 此参数用于解决一个坑,下面会说明 -->
                    <argLine>-noverify</argLine>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <formats>
                        <format>xml</format>
                        <format>html</format>
                    </formats>
                </configuration>
            </plugin>

采坑:

在使用 mvn cobertura:cobertura 命令生成测试覆盖率报告时,出现了如下问题(截取部分,报错原因如下):

  Reason:
    Expected stackmap frame at this location.
  Bytecode:
    0x0000000: 2ab4 001b 2bb9 002e 0200 c600 2f2a b400
    0x0000010: 1b2b b900 2e02 00c0 0030 b600 34c6 001c

解决方法:

命令介绍

maven-surefire-plugin 使用说明

Maven本身并不是一个单元测试框架,它只是在构建执行到特定生命周期阶段的时候,通过插件来执行JUnit或者TestNG的测试用例。这个插件就是maven-surefire-plugin,也可以称为测试运行器(Test Runner),它能兼容JUnit 3、JUnit 4以及TestNG。

在默认情况下,maven-surefire-plugin的test目标会自动执行测试源码路径(默认为src/test/java/)下所有符合一组命名模式的测试类。这组模式为:

maven-surefire-plugin 插件应用:

1.跳过测试

跳过测试运行 mvn package -DskipTests

或者通过 pom 提供该属性:

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.5</version>  
    <configuration>  
        <skipTests>true</skipTests>  
    </configuration>  
</plugin>

跳过测试代码的编译 mvn package -Dmaven.test.skip=true

或者通过 pom 提供该属性:

<plugin>  
    <groupId>org.apache.maven.plugin</groupId>  
    <artifactId>maven-compiler-plugin</artifactId>  
    <version>2.1</version>  
    <configuration>  
        <skip>true</skip>  
    </configuration>  
</plugin>

2. 动态指定要运行的测试用例

mvn test -Dtest=RandomGeneratorTest

也可以使用通配符:

mvn test -Dtest=Random*Test

或者也可以使用“,”号指定多个测试类:

mvn test -Dtest=Random*Test,AccountCaptchaServiceTest

如果没有指定测试类,那么会报错并导致构建失败:

mvn test -Dtest
这时候可以添加 -DfailIfNoTests=false 参数告诉 maven-surefire-plugin 即使没有任何测试也不要报错:

mvn test -Dtest -DfailIfNoTests=false

3. 包含与排除测试用例

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-surefire-plugin</artifactId>  
    <version>2.5</version>  
    <configuration>  
        <includes>  
            <include>**/*Tests.java</include>  
        </includes>  
        <excludes>  
            <exclude>**/*ServiceTest.java</exclude>  
            <exclude>**/TempDaoTest.java</exclude>  
        </excludes>  
    </configuration>  
</plugin>

参考文章:

https://my.oschina.net/huhaoren/blog/410437

https://www.cnblogs.com/qyf404/archive/2015/12/12/5040593.html

上一篇下一篇

猜你喜欢

热点阅读