Wiremock 模拟返回 HTTP 500

Wiremock mock returning HTTP 500

我正在使用 wireMock,我得到了 500 Internal Server Error 的一致响应,即使存根响应是 200 OK。

如果我调试,我看到连接总是关闭的。知道可能出了什么问题吗?或者我做错了什么。

这是测试

public class MyControllerTest {

@Autowired
MyController myController;

String id1 = "id1";
String id2 = "id2";
Integer offset = 0;
Integer limit = 1;
int port = 8080;
protected WireMockServer wireMockServer;

@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(port);

@Test
public void hosDriversHoursOfServiceGet() throws Exception {
    stubFor(WireMock.get(urlEqualTo("/path/endPoint"))
                    .withQueryParam("id1", equalTo(id1))
                    .withQueryParam("id2", equalTo(id2))
                    .withQueryParam("limit", equalTo(limit.toString()))
                    .withQueryParam("offset", equalTo(offset.toString()))
                    .willReturn(aResponse().withStatus(200).withBody((getJsonString()))));
Response response = myController.hosDriversHoursOfServiceGet(driverId, 1, 1);
    assertEquals(Integer.valueOf(1), response.getCount());
}

private String getJsonString() {
    return "{\"count\":1}";
}


}

试图模拟: http://localhost:8080/path/endPoint?id1={id1}&id2={id2}&limit={limit}&offset={offset}

上述调用是在客户端 class 中进行的,该客户端在 MyController 中自动装配。因为它不是直接调用,所以甚至可以模拟吗?

问题已解决,这与wiremock 和我的应用程序中的Javax-servlet-api 依赖项有关。将 wiremock 从 wiremock 2.6.0 更改为 wiremock-standalone,它使用应用程序的 servlet-api 依赖项,基本上它有效。

不确定这是否是正确的答案,但它奏效了。