使用dropwizard(4)-加入测试-jacoco代码覆盖率
来源:互联网 发布:樱井知香种子9部密码 编辑:程序博客网 时间:2024/06/03 17:33
前言
dropwizard提供了一个简单的测试框架。这里简单集成并加入jacoco测试。
作者:@Ryan-Miao
本文为作者原创,转载请注明出处:http://www.cnblogs.com/woshimrf/p/dropwizard-test-jacoco.html
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 0
- 使用dropwizard(4)-加入测试-jacoco代码覆盖率
- jacoco统计自动化测试代码覆盖率
- sonar 集成代码测试覆盖率插件jacoco
- Jacoco覆盖率工具使用(已测试)
- Jacoco覆盖率工具使用(已测试)
- maven工程加入jacoco检测单元测试代码覆盖率
- 代码覆盖率-JaCoCo
- Android UI测试框架Espresso以及代码覆盖率jacoco的使用
- 【android测试】jacoco做功能测试代码覆盖率统计
- Jacoco覆盖率工具使用
- jacoco测试覆盖率maven配置
- 实用Jacoco代码覆盖率Android集成与使用
- 使用MAVEN+Jacoco统计单元测试的代码覆盖率
- android项目的JaCoCo代码覆盖率入门使用
- Jacoco覆盖率工具使用调研
- 使用dropwizard(5)--加入swagger
- sonar ant+jacoco 编译测试并出具测试代码覆盖率报告
- Android单元测试/Ui测试+JaCoCo覆盖率统计
- 查找整数
- 函数模板
- Mac Vue.js 安装并初始化项目
- 在Django中自定义filter并在template中使用
- Aras innovator: TOC目录简单汉化方法
- 使用dropwizard(4)-加入测试-jacoco代码覆盖率
- 11.25作业(ZLY)
- 顺时针打印矩阵
- 网络原理,以及对VMware Workstation虚拟网络VMnet0、VMnet1、VMnet8的图解
- git添加公钥私钥完成ssh无密连接
- java web开发的毕业设计论文管理系统
- GridView使用
- 自顶向下,逐步求精------一条编程的捷径
- Lua笔记