使用dropwizard(4)-加入测试-jacoco代码覆盖率

前言

dropwizard提供了一个简单的测试框架。这里简单集成并加入jacoco测试。

Demo source

https://github.com/Ryan-Miao/l4dropwizard

本文是基于dropwizard入门之上的演进。

确保依赖都是最新的,或者自行解决版本冲突,比如jackson不同版本之间的类有所不同。

加入dropwizard-testing

在dependencies中增加依赖

<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-testing</artifactId>
    <version>${dropwizard.version}</version>
    <scope>test</scope>
</dependency>

新增Mockito

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.12.0</version>
    <scope>test</scope>
</dependency>

新增jacoco

在properties下新增

<jacoco.skip.instrument>true</jacoco.skip.instrument>
<jacoco.percentage.instruction>0.01</jacoco.percentage.instruction>
<jacoco.percentage.branch>0</jacoco.percentage.branch>

在plugin新增

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/ioc/**/*</exclude>
            <exclude>**/exceptions/**/*</exclude>
            <exclude>**/connector/*</exclude>
            <exclude>**/valueobject/**/*</exclude>
            <exclude>**/exception/**/*</exclude>
            <exclude>**/entity/**/*</exclude>
            <exclude>**/constant/*</exclude>
            <exclude>**/*Test.*</exclude>
            <exclude>**/Dagger*</exclude>
            <exclude>**/*Factory.*</exclude>
            <exclude>**/*Module.*</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-check</id>
            <phase>test</phase>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <element>BUNDLE</element>
                        <limits>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>INSTRUCTION</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>${jacoco.percentage.instruction}</minimum>
                            </limit>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>BRANCH</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>${jacoco.percentage.branch}</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
        <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-instrument</id>
            <phase>test</phase>
            <goals>
                <goal>instrument</goal>
            </goals>
            <configuration>
                <skip>${jacoco.skip.instrument}</skip>
            </configuration>
        </execution>
    </executions>
</plugin>

编写测试

首先,更新依赖,

mvn clean install

IDEA中刷新maven按钮。

然后,新建Resource测试类:

package com.test.domain.resource;

import com.test.domain.entiry.GithubUser;
import com.test.domain.service.IGithubService;
import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by Ryan Miao on 11/20/17.
 */
public class GithubResourceTest {

    private static final IGithubService service = mock(IGithubService.class);

    @ClassRule
    public static final ResourceTestRule resources = ResourceTestRule.builder()
            .addResource(new GithubResource(service))
            .build();


    @Before
    public void setup() {

    }

    @After
    public void tearDown(){
        // we have to reset the mock after each test because of the
        // @ClassRule, or use a @Rule as mentioned below.
        reset(service);
    }

    @Test
    public void testGetPerson() {

        GithubUser user = new GithubUser();
        String name = "Ryan";
        user.setName(name);
        when(service.getUserProfile(anyString())).thenReturn(user);
        GithubUser githubUser = resources.target("/github/users/ryan-miao").request().get(GithubUser.class);
        assertEquals(name, githubUser.getName());
        verify(service).getUserProfile("ryan-miao");
    }

}

验收,查看覆盖率

mvn clean install

查看jacoco覆盖率

report在target/site/jacoco/index.html

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3095
来自专栏java 成神之路

使用 NIO 实现 echo 服务器

4537
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

5346
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

4818
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2506
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

2938
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2537
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

30910
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2697
来自专栏Golang语言社区

【Golang语言社区】GO1.9 map并发安全测试

var m sync.Map //全局 func maintest() { // 第一个 YongHuomap := make(map[st...

4688

扫码关注云+社区