【问题标题】:Spring cloud contract best practice to handle unhappy pathSpring Cloud Contract 处理不愉快路径的最佳实践
【发布时间】:2018-02-16 20:11:55
【问题描述】:

我们曾经使用wiremock 进行集成测试,其中包含快乐和不快乐的路径。现在我们正在尝试转向基于 Spring Cloud 合约的集成测试。但是,我找不到任何与不愉快路径的合同相关的文档(状态代码超过 400)。我做了一些 POC,状态码为 4xx/5xx 作为响应,但没有奏效。

任何人都知道在消费者方面处理不愉快路径的最佳实践吗?还是完全不支持状态码超过 400 且带有 Spring Cloud 合约的不愉快路径?

【问题讨论】:

  • 不快乐的路径与快乐的路径完全相同。请记住,来自 DSL / MockMvc 测试的 Spring Cloud Contract 以与通过 WireMock API 创建它们的方式相同的方式生成 WireMock 存根。因此,如果存根说当一些数据被发送到它时它会返回 400,那么这也应该发生在消费者端。也许您应该将您的示例上传到某个地方,以便我可以尝试帮助您?

标签: spring cloud contract


【解决方案1】:

这是一个例子:

制作方

Contract.make {
    description 'get 404 when entity was not found'
    request {
        method GET()
        url '/entities/0'
    }
    response {
        status NOT_FOUND()
    }
}

客户端

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SomeApplication.class)
@AutoConfigureStubRunner(ids = "io.app:entity:+:stubs:8080")
@AutoConfigureTestDatabase
public class EntityClientTest {

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Autowired
    private EntityClient entityClient; // This is a FeignClient

    @Test
    public void shouldThrowNotFoundWithInvalidId() {
        exception.expect(FeignException.class);
        exception.expectMessage("404");

        entityClient.getById(0);
    }
}

如您所见,getById 抛出了 404,因为合同是这样写的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 2010-11-07
    • 2018-02-28
    • 2021-09-07
    • 2012-05-12
    • 1970-01-01
    相关资源
    最近更新 更多