我想用有效负载作为简单字符串来模拟响应。 下面的代码在我的测试中:
Response response = mock(Response.class, RETURNS_DEEP_STUBS);
when(mockRestClient.get("someMockUrl")).thenReturn(response);
when(response.readEntity(String.class)).thenReturn("XXXYYYZZZ");
也尝试过:
Response response = Response.status(Status.OK).entity("XXXYYYZZZ").build();
MyCode看起来像这样:
Response response = restClient.get(tokenUrl); // I was mocking this and getting mock response
String requestToken = response.readEntity(String.class); //failing at this line
以上都不起作用。 感谢帮助!
答案 0 :(得分:0)
我看到问题在于使用深层存根。就我而言,这是没有必要的。
Response mockResponse = mock(Response.class);
when(mockResponse.readEntity(String.class)).thenReturn("XXXYYYZZZ");