[英]how to mock sun jersey client post calls?
This is my code 这是我的代码
@Service
public class PaymentHandler {
private static final Gson GSON = new Gson();
private static Client webServiceClient = createSslClient(); // function creates a ssl connection
public Response makePayment(String payload) {
WebResource webResource = webServiceClient.resource(url);
WebResource.Builder builder = webResource.getRequestBuilder();
String r = builder
.type(MediaType.APPLICATION_JSON_TYPE)
.accept(MediaType.APPLICATION_JSON_TYPE)
.post(String.class, payload);
Response response = GSON.fromJson(r, Response.class);
}
}
Here is how I try to test it which doesn't work , it always makes a call to the payment service. 以下是我尝试测试不起作用的方法,它总是拨打支付服务电话。 I am unable to mock it.
我无法嘲笑它。
Client client = mock(Client.class );
WebResource webResource = mock(WebResource.class);
WebResource.Builder builder = mock(WebResource.Builder.class);
ClientResponse clientResponse = mock(ClientResponse.class);
when(client.resource(anyString())).thenReturn(webResource);
when(webResource.getRequestBuilder()).thenReturn(builder);
when(builder.type(anyString())).thenReturn(builder);
when(builder.accept(anyString())).thenReturn(builder);
when(builder.post(Matchers.eq(String.class), anyString())).thenReturn("Test");
paymentHandler.makePayment(payload); //assume that I send actual payload
Can someone please tell me how to mock this ? 有人可以告诉我如何嘲笑这个?
In your test, I don't see that you replace the webServiceClient
with mocked version. 在您的测试中,我没有看到您使用模拟版本替换
webServiceClient
。
But first of all, I believe you are better off not writing such code as PaymentHandler
without dependency injection. 但首先,我相信你最好不要在没有依赖注入的情况下编写像
PaymentHandler
这样的代码。 It could be just a simple composition with webServiceClient
being injected into the PaymentHandler
. 它可能只是一个简单的组合,
webServiceClient
被注入到PaymentHandler
。 Without dependency injection it's not flexible, hardly maintainable and as a result not testable. 没有依赖注入,它不灵活,难以维护,因此不可测试。 Imagine, for example, what would happen if initialization of such a field required some interaction with external system.
例如,想象一下,如果这种字段的初始化需要与外部系统进行一些交互会发生什么。 How would you test it without any byte-code-manipultaing libraries?
如果没有任何字节码操作库,您将如何测试它? Or how would you easily migrate from one
webServiceClient
to another, eg from non-ssl to ssl? 或者您如何轻松地从一个
webServiceClient
迁移到另一个webServiceClient
,例如从非ssl迁移到ssl?
Despite these well-known problems, sometimes we have to deal with 3rd-party or legacy code which we can't easily change. 尽管有这些众所周知的问题,但有时我们必须处理我们无法轻易改变的第三方或遗留代码。 But we want to write tests for the code which interacts with that 3rd-party code.
但我们希望为与第三方代码交互的代码编写测试。 For this exact reason, there exist some cool test frameworks.
出于这个原因,存在一些很酷的测试框架。 PowerMock is one of them and below is the working code using that it:
PowerMock就是其中之一,下面是使用它的工作代码:
@RunWith(PowerMockRunner.class)
@PrepareForTest(PaymentHandler.class)
public class PaymentHandlerTest {
@Test
public void test() throws Exception {
//we don't want to initialize the PaymentHandler.class because it might cause some
//heavy undesirable initilization. E.g. if we had referred to PaymentHandler as a class
//literal here, then the webServiceClient would've been initializaed with some "real"
//instance of Client. My PaymentHandler is located in so package. You should specify your
//fully qualified class' name here
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass("so.PaymentHandler");
//now the webServiceClient will be null once we initialize the PaymentHandler class
PowerMockito.suppress(PowerMockito.method(clazz, "createSslClient"));
Client client = mock(Client.class);
//here we initialize the PaymentHandler.class and finally mock the webServiceClient
Whitebox.setInternalState(clazz, "webServiceClient", client);
PaymentHandler paymentHandler = new PaymentHandler();
WebResource webResource = mock(WebResource.class);
WebResource.Builder builder = mock(WebResource.Builder.class);
when(client.resource(anyString())).thenReturn(webResource);
when(webResource.getRequestBuilder()).thenReturn(builder);
//take note of any(MediaType.class) instead of anyString() from your example. As in
//your PaymentHandler, MediaType is used instead of String
when(builder.type(any(MediaType.class))).thenReturn(builder);
when(builder.accept(any(MediaType.class))).thenReturn(builder);
when(builder.post(Matchers.eq(String.class), anyString())).thenReturn("{}");
paymentHandler.makePayment("payload");
}
}
In my example, I used the following dependencies: 在我的示例中,我使用了以下依赖项:
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.0'
These are the latest versions but earlier versions can do it as well 这些是最新版本,但早期版本也可以
Here is how I used mocked it 这是我用来嘲笑它的方式
@Mock
Client client;
@Mock
WebResource webResource;
@Mock
WebResource.Builder builder;
@Test
public void test() {
ReflectionTestUtils.setField(payeezyHandler,"webServiceClient",client);
Mockito.when(client.resource(anyString())).thenReturn(webResource);
Mockito.when(webResource.getRequestBuilder()).thenReturn(builder);
Mockito.when(builder.type(MediaType.APPLICATION_JSON_TYPE)).thenReturn(builder);
Mockito.when(builder.accept(MediaType.APPLICATION_JSON_TYPE)).thenReturn(builder);
Mockito.when(builder.post(Matchers.eq(String.class),anyString())).thenReturn(fakeResponse());
}
I know that ReflectionTestUtils are bad to use. 我知道ReflectionTestUtils不好用。 But if your test class has just one public function to test, then I guess there is no harm.
但是如果你的测试类只有一个公共函数来测试,那么我猜没有任何伤害。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.