标签:rac 测试 script mode runner equal 消费者 bdd ade
主要用于在微服务架构下做CDC(消费者驱动契约)测试。下图展示了多个微服务的调用,如果我们更改了一个模块要如何进行测试呢?
模拟一个股票价格查询的服务
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 微服务契约测试框架
标签:rac 测试 script mode runner equal 消费者 bdd ade
原文地址:https://www.cnblogs.com/freshchen/p/12229111.html