消费者测试中对于有效和无效要求的误报(匹配多个合同)

时间:2019-04-29 20:58:18

标签: spring-cloud-contract

在这种情况下,根据对请求的要求,生产者端有两种不同的结果,一种是成功,另一种是引发错误消息。

具有两个合同的简化示例: 1)Contract.make {     请求{

    method PUT()
    urlPath("/sample")
    headers {
        contentType('application/json')
    }
    body("{\"acc\": \"1234A\" ,\"case\":\"abc23\",\"re\":2018/12/12}")

}


response {
    status BAD_REQUEST()
       }

} 2) Contract.make {     请求{

    method PUT()
    urlPath("/sample")
    headers {
        contentType('application/json')
    }
    body("{\"acc\": \"1234\" ,\"case\":\"abc23\",\"re\":2018/12/12}")

}


response {
    status 200
       }

}

在消费者方面,当我运行无效的请求测试用例时,它可以匹配两个请求,其中as为org.springframework.web.client.HttpClientErrorException $ BadRequest:400 Bad Request

但是对于这两种情况,我都可以看到请求和响应响应,我可以在日志中看到

有人可以帮助我吗?

谢谢

这些是我的消费者测试用例

1)它的成功请求scenauro的工作正常,它会得到200

enter code here
     @Test
     public void should_update_case_sucess() throws Exception {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Content-Type", "application/json");
    ResponseEntity<String> response = restTemplate.exchange(
          "http://localhost:8083//sample",
            HttpMethod.PUT,
            new HttpEntity<>("{\"acc\":\"1234\",\"case\":\"abc23\",\"re\":\"20181212\"}", httpHeaders), String.class);
    BDDAssertions.then(response.getStatusCodeValue()).isEqualTo(200);


}

2) 这是失败的情况,没有得到400个响应,而是出现了httpclient错误,无法调用目标

enter code here
   @Test
   public void should_update_case_error() throws Exception {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Content-Type", "application/json");
    ResponseEntity<String> response = restTemplate.exchange(
            "http://localhost:8083//sample",
            HttpMethod.PUT,
            new HttpEntity<>("{\"acc\":\"1234A\",\"caseNumber\":\"abc23\",\"representmentStartDate\":\"20181212\"}", httpHeaders), String.class);

 BDDAssertions.then(response.getStatusCodeValue()).isEqualTo(400);

}

您能帮我吗

1 个答案:

答案 0 :(得分:0)

这不起作用,因为Wiremock具有两个相同的请求和两个不同的响应,因此第一个获胜。

您需要做的是警告请求以区分两者,这样您就可以找到正确的响应。

相关问题