经过前面几次文章的分享的UT的相关知识,今天接着分享UT相关最后一测文章,希望对大家在UT的学习中有一点点的帮助。

Spring集成测试

有时候我们需要在跑起来的Spring环境中验证,Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的集成测试。

在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启和使用它。

这时首先就有了Spring容器运行环境,就可以模拟浏览器调用等操作

引入测试坐标

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
<scope>test</scope>
</dependency>

为了与生产环境配置区分开

新建一个application-test.yml

server:
port: 8088
spring:
application:
name: hello-service-for-test

controller类,也就是被测对象

@RestController
public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() throws Exception {
return "Hello World";
}
}

测试方案一

通过TestRestTemplate模拟调用Rest接口

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class HelloControllerTest { @Autowired
private TestRestTemplate restTemplate; @Value("${spring.application.name}")
private String appName; @BeforeEach
void setUp() {
assertThat(appName).isEqualTo("hello-service-for-test");
} @Test
void testHello() throws Exception {
String response = restTemplate.getForObject("/hello", String.class); assertThat(response).isEqualTo("Hello World");
}
}

测试方案二

通过MockMvc来调用Rest接口

@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class HelloControllerTest { @Autowired
private HelloController helloController; @Autowired
private MockMvc mockMvc; @Value("${spring.application.name}")
private String appName; @BeforeEach
void setUp() {
assertThat(appName).isEqualTo("hello-service-for-test");
} @Test
void testHello() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/hello"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("Hello World"));
}
}

方案一会启动Spring容器,相对更符合我们测试思路,建议选用此方案测试

方案二不会启动内置的容器,所以耗时相对少一点

与Spring类似dropwizard也有一套测试方案,可以提供Jetty容器来做集成测试

Dropwizard集成测试

引入maven坐标

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>2.0.21</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
<scope>test</scope>
</dependency>

被测试资源类

@Path("/ping")
@Service
public class PingResource {
@GET
public String ping() {
return "pong";
}
}

测试方案一

不启动Jetty容器,通过ResourceExtension扩展测试

@ExtendWith(DropwizardExtensionsSupport.class)
class PingResourceTest { private static final ResourceExtension EXT = ResourceExtension.builder()
.addResource(new PingResource())
.build(); @Test
void should_get_pong_when_send_ping() {
String response = EXT.target("/ping").request().get(String.class); assertThat(response).isEqualTo("pong");
}
}

测试方案二

通过启动Jetty容器测试,为了避免项目中的循环依赖关系或加快测试运行速度,可以通过将JAX-RS资源编写为测试DropwizardClientExtension来测试HTTP客户端代码,并启动和停止包含测试的简单Dropwizard应用程序。

@ExtendWith(DropwizardExtensionsSupport.class)
class PingResourceTest3 { private static final DropwizardClientExtension EXT = new DropwizardClientExtension(new PingResource()); @Test
void should_get_pong_when_send_ping() throws IOException {
URL url = new URL(EXT.baseUri() + "/ping");
System.out.println(url.toString());
String response = new BufferedReader(new InputStreamReader(url.openStream())).readLine(); assertThat(response).isEqualTo("pong");
}
}

测试方案三

通过指定yml配置文件,Jersey HTTP client调用Rest接口, 返回的客户端可以在测试之间重用

在JUnit5测试类中添加DropwizardExtensionsSupport注释和DropwizardAppExtension扩展名将在运行任何测试之前启动应用程序

并在测试完成后再次停止运行(大致等同于使用@BeforeAll@AfterAll

DropwizardAppExtension也暴露了应用程序的ConfigurationEnvironment并且应用程序对象本身,使这些可以通过测试进行查询。

新增配置文件\src\test\resources\hello.yml

server:
type: simple
rootPath: '/api/*'
applicationContextPath: /
connector:
type: http
port: 9090

测试:

@ExtendWith(DropwizardExtensionsSupport.class)
class PingResourceTest2 { private static DropwizardAppExtension<HelloWorldServiceConfiguration> EXT = new DropwizardAppExtension<>(
HelloWorldServiceApp.class,
ResourceHelpers.resourceFilePath("hello.yml")
); @Test
void should_get_pong_when_send_ping() {
Client client = EXT.client();
String response = client.target(
String.format("http://localhost:%d/api/ping", EXT.getLocalPort()))
.request()
.get(String.class); assertThat(response).isEqualTo("pong");
}
}

参考

https://www.dropwizard.io/en/latest/manual/testing.html#

前文传送门

1、工作多年后我更了解了UT的重要性

2、五年了,你还在用junit4吗?

UT之最后一测的更多相关文章

  1. 关于UT的一些总结

    本文是个人对于UT的一些想法和总结,参考时建议请查阅官方资料. 转载请注明出处:http://www.cnblogs.com/sizzle/p/4476392.html 测试思想 编写UT测试代码,通 ...

  2. [UT]Unit Test理解

    Coding中有一个原则:Test Driven Development.  UT中的一些基本概念: 1. 测试驱动 2. 测试桩 3. 测试覆盖 4. 覆盖率 单体测试内容: 1. 模块接口:测试模 ...

  3. 输入输出无依赖型函数的GroovySpock单测模板的自动生成工具(上)

    目标 在<使用Groovy+Spock轻松写出更简洁的单测> 一文中,讲解了如何使用 Groovy + Spock 写出简洁易懂的单测. 对于相对简单的无外部服务依赖型函数,通常可以使用 ...

  4. 【阿里云产品公测】PTS压力测试最低配ECS性能及评测

    PTS是一个性能测试工具,可以使用PTS对自身系统性能在阿里云环境里的状况进行整体评估来找出你的系统性能瓶颈从而优化系统,同时你还可以在了解自己的系统性能指标情况下便于未来新增扩容.在使用PTS前你必 ...

  5. UT技巧

    (一)PowerMockito进行UT测试如何略过方法,使方法不被执行(含私有方法): PowerMockito.doNothing().when(TestMock.class,"foo1& ...

  6. 多测师_测试理轮_002(v模型和H模型)

    为什么要测试?1.软件非正常运行或自身缺陷会引发问题2.代码和文档是人写的,难免会出错3.环境原因影响软件(内存不足,存储,数据库溢出等)4.软件测试活动是保证软件质量的关键之一 什么是测试?软件行业 ...

  7. mysql每秒最多能插入多少条数据 ? 死磕性能压测

    前段时间搞优化,最后瓶颈发现都在数据库单点上. 问DBA,给我的写入答案是在1W(机械硬盘)左右. 联想起前几天infoQ上一篇文章说他们最好的硬件写入速度在2W后也无法提高(SSD硬盘) 但这东西感 ...

  8. 强强联合,Testin云测&云层天咨众测学院开课了!

    Testin&云层天咨众测学院开课了! 共享经济时代,测试如何赶上大潮,利用碎片时间给女票或者自己赚点化妆品钱?   2016年12月13日,Testin联手云层天咨带领大家一起推开众测的大门 ...

  9. nginx代理https站点(亲测)

    nginx代理https站点(亲测) 首先,我相信大家已经搞定了nginx正常代理http站点的方法,下面重点介绍代理https站点的配置方法,以及注意事项,因为目前大部分站点有转换https的需要所 ...

随机推荐

  1. SpringBoot注解集合

    使用注解的优势: 1.采用纯java代码,不在需要配置繁杂的xml文件 2.在配置中也可享受面向对象带来的好处 3.减少复杂配置文件的同时亦能享受到springIoC容器提供的功能 @SpringBo ...

  2. Java基础自学小项目

    实现一个基于文本界面的<家庭记账软件> 需求:能够记录家庭的收入,支出,并能够收支明细表 主要涉及一下知识点: - 局部变量和基本数据类型 - 循环语句 - 分支语句 - 方法调用和返回值 ...

  3. Hi3559AV100外接UVC/MJPEG相机实时采图设计(二):V4L2接口的实现(以YUV422为例)

    下面将给出Hi3559AV100外接UVC/MJPEG相机实时采图设计的整体流程,主要实现是通过V4L2接口将UVC/MJPEG相机采集的数据送入至MPP平台,经过VDEC.VPSS.VO最后通过HD ...

  4. 《深入浅出WPF》-刘铁猛学习笔记——XAML

    XAML是什么? XAML是微软公司创造的一种开发语言,XAML的全称是 Extensible Application Markup Language,即可拓展应用程序标记语言. 它由XML拓展而来, ...

  5. AJAX 相关参数详细说明

    最近ajax的使用十分频繁,对其许多参数还不是很了解,特此总结. 通用写法 1 $.ajax({ 2 url: "http://www.hzhuti.com", //请求的url地 ...

  6. React函数式组件和类组件[Dan]

    一篇对Dan的 How Are Function Components Different from Classes? 一文的个人阅读总结,内容来自于此.强烈推荐阅读 Dan Abramov.的博客. ...

  7. Java 语言基础 02

    语言基础·二级 顺序结构语句 * A:什么是流程控制语句    * 流程控制语句:可以控制程序的执行流程. * B:流程控制语句的分类    * 顺序结构    * 选择结构    * 循环结构 *  ...

  8. C++高精度计算(大整数类)

    Java和Pathon可以不用往下看了 C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63-2^63-1](对应8个字节所对应的二进制数大小).但是对于某些需 ...

  9. WPF 基础 - Binding 对数据的转换和校验

    1. Binding 对数据的转换和校验 Binding 中,有检验和转换关卡. 1.1 数据校验 源码: namespace System.Windows.Data { public class B ...

  10. Java线程安全问题

    线程安全问题是一个老生常谈的问题,那么多线程环境下究竟有那些问题呢?这么说吧,问题的形式多种多样的,归根结底的说是共享资源问题,无非可见性与有序性问题. 1. 可见性 可见性是对于内存中的共享资源来说 ...