Wiremock 模拟返回 HTTP 500

Pav*_*otk 4 junit4 wiremock

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

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

这是测试

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}";
}


}
Run Code Online (Sandbox Code Playgroud)

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

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

Pav*_*otk 6

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

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