简介

使用场景

主要用于在微服务架构下做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. The call() and apply() Mtheods

    Example 6-4function classof(o) {     if (o === null) return "Null";     if (o ===undefined ...

  2. 喵喵电影git操作

    1.git remote 2.git remote add origin '项目地址'   (origin为远程仓库名字) 3.git remote 4.git push origin master ...

  3. 2018-2-13-win10-UWP-Markdown-含源代码

    title author date CreateTime categories win10 UWP Markdown 含源代码 lindexi 2018-2-13 17:23:3 +0800 2018 ...

  4. 5.29 SD省队培训D1

    5.29 SD省队培训D1 自闭的一天 T1 梦批糼 先咕一咕(两天之内一定补上) T2 等你哈苏德 继续咕(一星期之内补上) T3喜欢最最痛 四十分做法: 首先,我们发现同一个点加两条额外边是一件非 ...

  5. eclipse要修改的配置

    1. 修改 html的字体大小 window->preferences->General--> appearance--> Colors and fonts-->basi ...

  6. basedir的介绍

    os内置dirname方法就是文件目录的往上找一级 #得到当前文件名或者路径 if __name__ == "__main__": print(__file__) #这是os的一个 ...

  7. Kubernetes从私有镜像仓库中拉取镜像

    当我们尝试从私有仓库中拉取镜像时,可能会收到这样提示:requested access to the resource is denied Error response from daemon: pu ...

  8. jquery中为动态增加的元素添加事件

    // html代码 <ul id="main"> </ul> // js代码 $(function(){ // 动态添加html代码 $("#ma ...

  9. codeforces -1214 E

    题目https://codeforces.com/problemset/problem/1214/E 就是得知奇数之间不产生影响,先造出一条链,再用偶数往里插就行.链要di从大到小排个序呀!! #in ...

  10. 使用vue 对二进制文件 实现下载(blob对象

    有很多网站会涉及到文件下载,这里我们使用axios 发送请求 接受数据 第一步 模仿jQ 封装接口 Vue.prototype.$xlsx_post = function (url, data, fu ...