嘲笑响应课和使用读取方法阅读

发布于 2025-02-09 10:56:00 字数 611 浏览 0 评论 0原文

我需要嘲笑一个具有返回javax.ws.rs.core.response的方法的类。该类遵循此方法调用。我将响应设置为如下:

when(methodCall).thenReturn(Response.status(Response.Status.OK).entity(someEntity).build());

现在,当使用Response..ReadEntity(String.Class)读取此响应时;它引发了一个错误:

java.lang.illegalstateException:在出站消息上不支持的方法。

我如何设置theTreturn中的响应,以返回readentity()方法可读取的入口消息。

PS我只能在测试类中进行更改。 我正在使用Junit 4.12和Mockito-All 1.9.5,Mockito-Inline 3.8.0

我正在嘲笑的类:

Response response = client.target().request().header().post();
String entity = response.readEntity(String.class);

I need to mock a class that has a method that returns javax.ws.rs.core.Response. The class follows this method call with a .readEntity(String.class). I'm setting the Response as follows:

when(methodCall).thenReturn(Response.status(Response.Status.OK).entity(someEntity).build());

Now when this Response is being read using response.readEntity(String.class); it throws an error:

java.lang.IllegalStateException : Method not supported on an outbound message.

How can I set the Response in thenReturn so as to return an inbound message readable by the readEntity() method.

P.S. I can't make changes in the main class, only in test class.
I'm using java 1.8 with junit 4.12 and mockito-all 1.9.5, mockito-inline 3.8.0

The class I'm mocking:

Response response = client.target().request().header().post();
String entity = response.readEntity(String.class);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅过去的我 2025-02-16 10:56:00

首先,您必须确保对响应进行模拟:

Response response = Mockito.mock(Response.class)

然后,我建议您以这种方式模拟READENTITY

Mockito.doReturn(Response.status(Response.Status.OK).entity(someEntity).build())
       .when(response)
       .readEntity(String.class);

First of all, you have to be sure that the response is mocked:

Response response = Mockito.mock(Response.class)

Then I suggest you to mock readEntity in this way:

Mockito.doReturn(Response.status(Response.Status.OK).entity(someEntity).build())
       .when(response)
       .readEntity(String.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文