如何模拟response.readEnity(String.class),传递模拟Response对象

时间:2019-09-12 21:06:09

标签: rest mocking mockito junit5

我想用有效负载作为简单字符串来模拟响应。 下面的代码在我的测试中:

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

以上都不起作用。 感谢帮助!

1 个答案:

答案 0 :(得分:0)

我看到问题在于使用深层存根。就我而言,这是没有必要的。

Response mockResponse = mock(Response.class);
when(mockResponse.readEntity(String.class)).thenReturn("XXXYYYZZZ");