Spring云契约处理不愉快路径的最佳实践

时间:2018-02-16 20:11:55

标签: spring cloud contract

我们过去常常使用wiremock进行集成测试,包括快乐和不快乐的路径。现在我们正在努力转向基于Spring云合同的集成测试。虽然,我找不到任何与不愉快路径合同相关的文件(状态代码超过400)。我做了一些状态代码为4xx / 5xx的POC作为回应,但它没有用。

任何人都知道处理消费者方面不愉快路径的最佳做法?或者,对于状态代码超过400的不幸路径,使用spring cloud合同,它是否完全不受支持?

1 个答案:

答案 0 :(得分: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,因为合同如此。

相关问题