我正在与WIREMOCK一起编写一些测试。当我执行测试用例时,我得到下面的错误。
请求不匹配
Closest stub | Request
GET | CONNECT <<<<< HTTP method does not match
/greeting | null <<<<< URL does not match
这是我的代码。
public class HelloTest {
@Autowired
HelloDao HelloDao;
@Test
void testMethod() {
String jsonResponse = """
{
"version": {
"id": 22
}
}
""";
WIREMOCK.stubFor(get("/greeting").willReturn(aResponse().withStatus(HttpStatus.OK.value())
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withBody(jsonResponse)));
var response = assertInstanceOf(HelloResp.class, HelloDao.getGreeting());
}
}
public HelloResp getGreeting() {
CompletableFuture<HelloResp> future = HelloAsyncDao.getGreeting();
return ExternalApiUtils.joinFuture(future, HelloProperties.VERSION_API);
}
public CompletableFuture<HelloResp> getGreeting() {
String urlPart = "/greeting";
HttpRequest httpRequest = HttpRequest.builder(urlPart).get().addHeaders(generateHttpHeaders()).build();
return httpService.execute(httpRequest, helloProperties.getHttp(),HelloProperties.VERSION_API, HelloResponseMapper::mapHelloRespResp);
}
我更改了URI并尝试了一些试错方法。我不知道这个错误是如何发生的,因为我是相当新的wiremock。如果能帮上忙我会很感激的。先谢谢你。
1条答案
按热度按时间kkbh8khc1#
您应该在之前设置wiremock并使用spring Boot test annotations。下面是一个工作示例:
l