前言

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. Android ViewPager实现图片标题轮播和点击事件

    我看别人的文章,最喜欢看有图的.然后先找图看. 那我就先上效果图: 实现了图片自动轮播,手动滑动,轮播标题,以及点击事件.下面开始: 一.资源文件: 首先是布局: <?xml version=& ...

  2. python爬虫:使用Selenium模拟浏览器行为

    前几天有位微信读者问我一个爬虫的问题,就是在爬去百度贴吧首页的热门动态下面的图片的时候,爬取的图片总是爬取不完整,比首页看到的少.原因他也大概分析了下,就是后面的图片是动态加载的.他的问题就是这部分动 ...

  3. lua游戏开发实践指南学习笔记1

    本文是依据lua游戏开发实践指南做的一些学习笔记,仅用于继续自己学习的一些知识. Lua基础 1.  语言定义: 在lua语言中,标识符有非常大的灵活性(变量和函数名),只是用户不呢个以数字作为起始符 ...

  4. centos 6.5 安装mongodb2.6

    前言: 系统版本号:Centos-6.5-x86_64 *** Centos编译安装mongodb 2.6 系统最好是64位的,才干更好发挥mongodb的性能 1.准备,下载源文件(二进制编译版) ...

  5. android:自己定义组合控件Weight(高仿猫眼底部菜单条)

    在我们实际开发其中.会碰见一些布局结构类似或者同样的界面.比如应用的设置界面.tabbutton界面等. 这时候.对于刚開始学习的人来说,xml里面一个个绘制出来也许是最初的想法.可能随着经验的积累, ...

  6. 关于Mac终端故障一直出现 [进程已完毕]

    终端已打开就出现以下信息.无法输入不论什么的命令 Last login: Mon Aug 18 10:00:36 on ttys000 [进程已完毕] 原因:不知谁改动了 终端->偏好设置-&g ...

  7. jstl常用语句

    1.select框中if选中,下面的语句实现从后台给过来一个category实体,如果category的categoryType为指定的值,则选中. <select class="fo ...

  8. (转)Maven依赖的jar包下载不了、jar更新不了的解决办法

    场景一: 使用Maven的同学可能偶尔会遇到这种情况:pom.xml中依赖了项目需要的某个jar文件,但是使用Maven –> update project 还是没办法下载该jar到项目中,你可 ...

  9. 在Office Add-in中实现单点登陆(SSO)

    作者:陈希章 发表于 2017年12月27日 这篇文章经过多次修改,终于在今天晚上写完了,演示用的范例代码也终于跑通了.因为这个SSO的功能目前只是Preview的状态,所以本篇文章严格参考了官方的文 ...

  10. 《程序员的思维修炼:开发认知潜能的九堂课》【PDF】下载

    <程序员的思维修炼:开发认知潜能的九堂课>[PDF]下载链接: https://u253469.ctfile.com/fs/253469-231196325 内容简介 运用一门程序设计语言 ...