简介

使用场景

主要用于在微服务架构下做CDC(消费者驱动契约)测试。下图展示了多个微服务的调用,如果我们更改了一个模块要如何进行测试呢?

  • 传统的两种测试思路

    • 模拟生产环境部署所有的微服务,然后进行测试

      • 优点

        • 测试结果可信度高
      • 缺点
        • 测试成本太大,装一整套环境耗时,耗力,耗机器
    • Mock其他微服务做端到端的测试
      • 优点

        • 不用装整套产品了,测的也方便快捷
      • 缺点
        • 需要写很多服务的Mock,要维护一大堆不同版本用途的simulate(模拟器),同样耗时耗力
  • Spring Cloud Contrct解决思路

    • 每个服务都生产可被验证的 Stub Runner,通过WireMock调用,服务双方签订契约,一方变化就更新自己的Stub,并且测对方的Stub。Stub其实只提供了数据,也就是契约,可以很轻量的模拟服务的请求返回。而Mock可在Stub的基础上增加验证

契约测试流程

  • 服务提供者

    • 编写契约,可以用Groovy DSL 脚本也可以用 YAML文件
    • 编写测试基类用于构建过程中插件自动生成测试用例
    • 生成的测试用例会自动运行,这时如果我么提供的服务不能满足契约中的规则就会失败
    • 提供者不断完善功能直到服务满足契约要求
    • 发布Jar包,同时将Stub后缀的jar一同发布
  • 服务消费者
    • 对需要依赖外部服务的接口编写测试用例
    • 通过注解指定需要依赖服务的Stub jar包
    • 验证外部服务没有问题

简单案例

服务提供者

模拟一个股票价格查询的服务

项目地址

springcloud-contract-provider-rest

项目结构

项目依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<scope>test</scope>
</dependency> <build>
<plugins>
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>2.2.1.RELEASE</version>
<extensions>true</extensions>
<configuration>
<!--用于构建过程中插件自动生成测试用例的基类-->
<baseClassForTests>
com.example.springcloudcontractproviderrest.RestBaseCase
</baseClassForTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

编写契约

既然是消费者驱动契约,我么首先需要制定契约,这里为了方便假设查询贵州茅台的股价返回值是固定的999,也可以通过正则等方式去限制返回值

Contract.make {
description "query by id should return stock(id,price)" request {
method GET()
url value {
// 消费者使用时请求任何 /stock/price/数字 都会被转为 /stock/price/600519
consumer regex('/stock/price/\\d+')
producer "/stock/price/600519"
}
} response {
status OK()
headers {
contentType applicationJson()
}
// 提供给消费者的默认返回
body([
id : 600519,
price: 999
]) // 服务端在测试过程中,body需要满足的规则
bodyMatchers {
jsonPath '$.id', byRegex(number())
jsonPath '$.price', byRegex(number())
}
}
}

测试基类

主要是加载环境,然后由于不是真实环境模拟了数据库查询

@SpringBootTest
@RunWith(SpringRunner.class)
public class RestBaseCase { @Autowired
private StockController stockController; @MockBean
private StockRepository stockRepository; @Before
public void setup() {
init();
RestAssuredMockMvc.standaloneSetup(stockController);
} private void init() {
Mockito.when(stockRepository.getStockById(600519)).thenReturn(new StockDTO(600519, "贵州茅台", 999L, "SH"));
} }

实现服务并测试

实现我们的服务功能,具体代码逻辑可以在项目地址中查看,然后测试看是否符合契约

mvn clean test

可以在生成(target)目录中找到 generated-test-sources 这个目录,插件为我们自动生成并且运行的case就在其中

public class StockTest extends RestBaseCase {

	@Test
public void validate_shoudReturnStockIdAndPrice() throws Exception {
// given:
MockMvcRequestSpecification request = given(); // when:
ResponseOptions response = given().spec(request)
.get("/stock/price/600519"); // then:
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.header("Content-Type")).matches("application/json.*"); // and:
DocumentContext parsedJson = JsonPath.parse(response.getBody().asString()); // and:
assertThat(parsedJson.read("$.id", String.class)).matches("-?(\\d*\\.\\d+|\\d+)");
assertThat(parsedJson.read("$.price", String.class)).matches("-?(\\d*\\.\\d+|\\d+)");
} }

发布

如果一切顺利就可以deploy了

服务消费者

模拟查询个人资产的服务,需要远程调用股票价格查询服务,计算总资产

项目地址

springcloud-contract-consumer-rest

项目结构

验证服务

编写测试用例验证服务

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureStubRunner(
ids = {"com.example:springcloud-contract-provider-rest:+:stubs:8880"},
stubsMode = StubRunnerProperties.StubsMode.LOCAL
)
public class StockApiTest { @Autowired
private StockApi stockApi; @Test
public void testStockApi() throws IOException {
StockPriceDTO stockPrice = stockApi.getStockPrice(600519).execute().body();
BDDAssertions.then(stockPrice.getId()).isEqualTo(600519);
BDDAssertions.then(stockPrice.getPrice()).isEqualTo(999); }
}

Spring Cloud Contract 微服务契约测试的更多相关文章

  1. Spring Cloud构建微服务架构(一)服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  2. Spring Cloud构建微服务架构(五)服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: 我们使用Spring Cloud Netflix中的Eureka实现了服务 ...

  3. Spring Cloud构建微服务架构 - 服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: alt 我们使用Spring Cloud Netflix中的Eureka实 ...

  4. 【微服务】使用spring cloud搭建微服务框架,整理学习资料

    写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...

  5. 基于Spring Cloud的微服务入门教程

    (本教程的原地址发布在本人的简书上:http://www.jianshu.com/p/947d57d042e7,若各位看官有什么问题或不同看法请在这里或简书留言,谢谢!) 本人也是前段时间才开始接触S ...

  6. 干货|基于 Spring Cloud 的微服务落地

    转自 微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的 ...

  7. 基于Spring Cloud的微服务落地

    微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的微服务 ...

  8. Spring Cloud与微服务构建:Spring Cloud简介

    Spring Cloud简介 微服务因该具备的功能 微服务可以拆分为"微"和"服务"二字."微"即小的意思,那到底多小才算"微&q ...

  9. Spring Cloud与微服务构建:微服务简介

    Spring Cloud与微服务构建:微服务简介 单体架构及其不足 1.单体架构简介 在软件设计中,经常提及和使用经典的3曾模型,即表示层.业务逻辑层和数据访问层. 表示层:用于直接和用户交互,也成为 ...

随机推荐

  1. H3C IP地址与子网掩码

  2. hihocoeder1384

    hihocoeder1384 算法竞赛进阶指南上的题目 我们肯定是吧最大值和最小值匹配,次大值和次小值匹配以此类推 首先,类似于区间覆盖的思想,我们对于一个\(L\),找到最大的满足条件的\(R\) ...

  3. C# 通过编程的方法在桌面创建回收站快捷方式

    基本所有的桌面都会存在回收站快捷方式,如果想要多创建几个快捷方式,点开就是回收站,请看本文的方法 在引用 Windows Script Host Object Model 这个 COM 方法之后可以使 ...

  4. LuoguP5464 缩小社交圈

    LuoguP5464 缩小社交圈 背景:洛谷七月月赛T4 题目大意给定\(n\)个点,每个点的权值对应着一个区间\([l_i,r_i]\),两个点\(i,j\)有边当且仅当他们权值的并集不为空集,问有 ...

  5. JS闭包机制实现为DOM元素循环添加事件

    HTML代码: <button type='button' class='btn' id='1'>按钮1</button> <button type='button' c ...

  6. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  7. 树莓派4安装ftp服务端

    vsftpd是开源的轻量级的常用ftp服务器.   1,安装vsftpd服务器 (约400KB) sudo apt-get install vsftpd     2,启动ftp服务 sudo serv ...

  8. Windows Server Core Remote Manage Hyper-V

    原帖:https://serverfault.com/questions/852144/how-do-i-remotely-manage-hyper-v-2016-standalone-via-win ...

  9. socket粘包问题及解决方案

    一.粘包问题 问题1: 无法确认对方发送过来数据的大小. 'client.py' import socket client = socket.socket() client.connect( ('12 ...

  10. spinor/spinand flash之高频通信延迟采样

    SPI协议 对于spinor和spinand flash,其通信总线为SPI总线. SPI有四种工作模式,对应不同的极性和相位组合 极性,一般表示为CPOL(Clock POLarity),即SPI空 ...