前言

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

使用dropwizard(4)-加入测试-jacoco代码覆盖率的更多相关文章

  1. 新开发项目Jacoco代码覆盖率

    一般只有新的项目才会去用JaCoCo工具看一下代码覆盖率, 一来看看测试有没有漏的测试用例 二来看看开发有没有留下冗余的代码 新开发项目Jacoco代码覆盖率后端接口打成jar包,进行启动 #exec ...

  2. JaCoCo 代码覆盖率工具(基于Maven+TestNG)

    JaCoco是一个代码覆盖率库. 官方网站:http://www.jacoco.org/ 安装: 以 Maven(http://www.testclass.net/maven/) 安装为例: < ...

  3. Jacoco 代码覆盖率,监控WEB项目

    转载:https://blog.csdn.net/u010469432/article/details/73283824 jacococ代码覆盖率,以客户端形式直接监控远程代码 使用理解 jacoco ...

  4. Jacoco代码覆盖率工具

    https://www.cnblogs.com/fnng/p/7923314.html https://my.oschina.net/wangmengjun/blog/974067

  5. Jacoco远程统计tomcat服务(Windows系统)的代码覆盖率

    Jacoco远程统计tomcat服务(Windows系统)的代码覆盖率 2017-09-21 目录 1 Jacoco的安装和设置  1.1 什么是Jacoco?  1.2 Jacoco安装  1.3 ...

  6. 《大话移动 APP 测试》

    <大话移动 APP 测试> wiki: Software testing 第5章 常用工具介绍和实践 Android.iOS Monkey Android SDK 提供的一个工具:发送伪随 ...

  7. 使用 coverlet 查看.NET Core应用的测试覆盖率

    代码覆盖(Code coverage)是软件测试中的一种度量,描述程式中源代码被测试的比例和程度,所得比例称为代码覆盖率. Visual Studio 2017的企业版可以直接查看测试的代码覆盖率, ...

  8. Node入门教程(13)第十一章:mocha单元测试+should断言库+istanbul覆盖率测试+art-template

    声明:以下为老马的全栈视频教程的笔记,如果需要了解详情,请直接配合视频学习.视频全部免费,视频地址:https://ke.qq.com/course/294595?tuin=1eb4a0a4 node ...

  9. UT, FT ,E2E 测试的意思

    前端实现自动化就要借助到unit和e2e端到端测试了 一.unit测试(FT 就是Fucntion Test 功能测试,  注意不是: funciton函数 ...fucntion功能   不一样哦  ...

随机推荐

  1. Java解决Hanoi问题

    package fa.ct; import java.util.Scanner; public class Hanoi { public static void hanoi(int num,char ...

  2. 好好写代码吧,没事别瞎B去创业!

    知乎上看到这个问题 正好最近想写篇关于此的文章,于是就回答了一波. 也贴到这里来,回答如下 : 本问题简直为我量身定制,做为一个正在创业中的苦逼少年,说说我是如何从鼓吹怂恿身边人创业转换成反对创业的. ...

  3. xxx金融后台管理系统详细版:包括本地开发调试详细步骤

    效果演示地址, github地址: demo演示:         1.About 此项目是 vue2.0 + element-ui + node+mongodb 构建的后台管理系统,所有的数据都是从 ...

  4. form表单提交和ajax提交优先级

    form中若定义action,那么,ajax将不能执行.form默认提交的请求优先级高于ajax

  5. 诡异的 &quot;password取回&quot; 邮件问题

    大部分系统中都有"找回password"的功能,我们的平台也做了此功能,用户可通过 短信,邮件 找回password. 当中对于邮件找回password的方式遇到奇特的问题.记录下 ...

  6. vim 命令整理(自己经常使用)

    vimm(vimsual)是Linux/UNIX系列OS中通用的全屏编辑器. vimm分为两种状态,即命令状态和编辑状态.在命令状态下.所键入的字符系统均作命令来处理.如:q代表退出,而编辑状态则是用 ...

  7. C++学习笔记24,方法重写与方法隐藏

    该博文仅用于交流学习.请慎用于不论什么商业用途.本博主保留对该博文的一切权利. 博主博客:http://blog.csdn.net/qq844352155 转载请注明出处: 方法重写.是指在子类中又一 ...

  8. public,private,protected,以及不写时的差别

    作用域public,private.protected.以及不写时的差别 这四个作用域的可见范围例如以下表所看到的. 说明:假设在修饰的元素上面没有写不论什么訪问修饰符,则表示friendly.不使用 ...

  9. html5视频标签

    <video width="200" height="200" poster="img/shamo.jpg" src="vi ...

  10. 字符串函数---atof()函数具体解释及实现(完整版)

    atof()函数 atof():double atof(const char *str ); 功 能: 把字符串转换成浮点数 str:要转换的字符串. 返回值:每一个函数返回 double 值.此值由 ...