使用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
使用dropwizard(4)-加入测试-jacoco代码覆盖率的更多相关文章
- 新开发项目Jacoco代码覆盖率
一般只有新的项目才会去用JaCoCo工具看一下代码覆盖率, 一来看看测试有没有漏的测试用例 二来看看开发有没有留下冗余的代码 新开发项目Jacoco代码覆盖率后端接口打成jar包,进行启动 #exec ...
- JaCoCo 代码覆盖率工具(基于Maven+TestNG)
JaCoco是一个代码覆盖率库. 官方网站:http://www.jacoco.org/ 安装: 以 Maven(http://www.testclass.net/maven/) 安装为例: < ...
- Jacoco 代码覆盖率,监控WEB项目
转载:https://blog.csdn.net/u010469432/article/details/73283824 jacococ代码覆盖率,以客户端形式直接监控远程代码 使用理解 jacoco ...
- Jacoco代码覆盖率工具
https://www.cnblogs.com/fnng/p/7923314.html https://my.oschina.net/wangmengjun/blog/974067
- Jacoco远程统计tomcat服务(Windows系统)的代码覆盖率
Jacoco远程统计tomcat服务(Windows系统)的代码覆盖率 2017-09-21 目录 1 Jacoco的安装和设置 1.1 什么是Jacoco? 1.2 Jacoco安装 1.3 ...
- 《大话移动 APP 测试》
<大话移动 APP 测试> wiki: Software testing 第5章 常用工具介绍和实践 Android.iOS Monkey Android SDK 提供的一个工具:发送伪随 ...
- 使用 coverlet 查看.NET Core应用的测试覆盖率
代码覆盖(Code coverage)是软件测试中的一种度量,描述程式中源代码被测试的比例和程度,所得比例称为代码覆盖率. Visual Studio 2017的企业版可以直接查看测试的代码覆盖率, ...
- Node入门教程(13)第十一章:mocha单元测试+should断言库+istanbul覆盖率测试+art-template
声明:以下为老马的全栈视频教程的笔记,如果需要了解详情,请直接配合视频学习.视频全部免费,视频地址:https://ke.qq.com/course/294595?tuin=1eb4a0a4 node ...
- UT, FT ,E2E 测试的意思
前端实现自动化就要借助到unit和e2e端到端测试了 一.unit测试(FT 就是Fucntion Test 功能测试, 注意不是: funciton函数 ...fucntion功能 不一样哦 ...
随机推荐
- 使用背景图修改radio、checkbox样式
如果觉得设置样式太麻烦,或者页面上选中的样式太复杂,也可以用背景图去修改样式<div class=""> <label><input type=&qu ...
- 海量服务实践──手 Q 游戏春节红包项目设计与总结(上篇)
导语 大哥说.今年手Q游戏的春节红包你来做.那该怎么做?以及怎么做才干让大哥放心?本文从后台的角度出发讲述了这个过程和方法.对于关键的前台部分也有所涉及. 文件夹 1.需求背景 1.1.红包类别 1. ...
- 学习vi和vim编辑器(5):越过基础的藩篱
本章将对之前学习的编辑命令如" c "." d "." y "等命令进行总结,并学习一些新的知识:其它进入vi的方法,利用缓冲区来存储拖曳或 ...
- 《C程序猿从校园到职场》勘误
(本人正在參加2015博客之星评选.诚邀你来投票,谢谢:username=zhouzxi">http://vote.blog.csdn.net/blogstar2015/candida ...
- F01:金融学第一定律:时间的价值
很小的时候,我们就听大人们讲过:时间就是金钱. 长大了,也觉得这句话应该是对的.我们很珍惜时间,但是似乎没有看到金钱的急剧增加啊.总之,这是模糊,体验感又不强的一句话. 既然谈到时间的价值,价值最容易 ...
- 两款主流项目管理软件PK,哪个更好用?
两款主流项目管理软件PK,哪个更好用? 一.产品介绍 JIRA是Atlassian公司出品的项目与事务跟踪工具,被广泛应用于缺陷跟踪.客户服务.需求收集.流程审批.任务跟踪.项目跟踪和敏捷管理等工作领 ...
- bootstrap插件summernote 的使用
一.有时Java工程中会用到summernote 编辑器,下面简单说一下引用 1.将summernote 相应的文件放到工程中(webapp下面) 2.建一个jsp文件,在文件中引入相应的js.css ...
- 自学Zabbix3.3-一个简单例子 添加Hosts并应用模板
Host 是 Zabbix 监控的基本载体,所有的监控项都是基于 host 的. 通过 Configuration->Hosts->Create Host 来创建监控设备 按提示填入 Na ...
- github not authorized eclipse 关于 代码不能提交到GitHub
eclipse/myeclipse > menu > window > preferences > general > security > content > ...
- Eclipse 插件安装、升级和卸载的方法
Eclipse 的插件可以装在内部,也可以装在外部,装在内部的方法很简单:把插件的features和plugins目录copy到eclipse的安装目录即可. eclipse和其插件升级比较频繁,用过 ...