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

2024-05-03 09:58

本文主要是介绍使用dropwizard(4)-加入测试-jacoco代码覆盖率,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

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);@ClassRulepublic static final ResourceTestRule resources = ResourceTestRule.builder().addResource(new GithubResource(service)).build();@Beforepublic void setup() {}@Afterpublic void tearDown(){// we have to reset the mock after each test because of the// @ClassRule, or use a @Rule as mentioned below.reset(service);}@Testpublic 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代码覆盖率的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/956468

相关文章

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

使用Python实现一键隐藏屏幕并锁定输入

《使用Python实现一键隐藏屏幕并锁定输入》本文主要介绍了使用Python编写一个一键隐藏屏幕并锁定输入的黑科技程序,能够在指定热键触发后立即遮挡屏幕,并禁止一切键盘鼠标输入,这样就再也不用担心自己... 目录1. 概述2. 功能亮点3.代码实现4.使用方法5. 展示效果6. 代码优化与拓展7. 总结1.

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Linux中的计划任务(crontab)使用方式

《Linux中的计划任务(crontab)使用方式》:本文主要介绍Linux中的计划任务(crontab)使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言1、linux的起源与发展2、什么是计划任务(crontab)二、crontab基础1、cro