简体   繁体   English

Mockito 模拟响应类

[英]Mockito mocking a Response class

When I try to mock a javax.ws.rs.core Response I get a error message:当我尝试模拟 javax.ws.rs.core Response我收到一条错误消息:

Unable to create a JAX-RS runtime Delegate无法创建 JAX-RS 运行时委托

Why does this happen?为什么会发生这种情况?

 Response response = Mockito.mock(Response.class);

But when I try to mock a HttpServletResponse there is no issue with that!但是当我尝试模拟 HttpServletResponse 时,这没有问题!

 HttpServletResponse response1 = Mockito.mock(HttpServletResponse.class);

You can try with a fake response like this:您可以尝试使用这样的虚假响应:

ResponseBuilder responseBuilder = Response.ok();
when(client.form(any(Form.class))).thenReturn(responseBuilder.entity("his is a string").build();

In this snippet "when" is a mockito method, and responseBuilder object return a simple string.在这个片段中,“when”是一个模拟方法,responseBuilder 对象返回一个简单的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM