使用Mockito的模拟球衣客户端 [英] Mocking Jersey Client using Mockito

查看:87
本文介绍了使用Mockito的模拟球衣客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用JUnit和Mockito测试Jersey Client 1.19版本.我正在努力获取或阅读实体.我不知道如何从那里继续,而且我也收到了IllegalStateException. 下面是我用来为客户端响应创建模拟对象的代码.

Hello I am testing Jersey Client 1.19 version using JUnit and Mockito. I am struggling on getting or reading the entity. I don't know how to proceed from there and i am also getting IllegalStateException. Below is the code that i am using to create mock objects for client response.

公共类MockJerseyClient {

public class MockJerseyClient {

private ClientConfiguration clientConfig;
private Client client;
private WebTarget webTarget;
private Invocation.Builder invocationBuilder;
private Response response;
private RetrieveBillingResponseXMLReader xmlReader;
private ResponseBuilder responseBuilder;

public MockJerseyClient(String uri, int status, String contentType, String content) {

    // Mock Objects
    clientConfig = Mockito.mock(ClientConfiguration.class);

    client = Mockito.mock(Client.class);
    clientConfig.createClient();

    webTarget = Mockito.mock(WebTarget.class);
    clientConfig.createWebResource(uri);

    invocationBuilder = Mockito.mock(Invocation.Builder.class);

    xmlReader = new RetrieveBillingResponseXMLReader();

    responseBuilder = Response.accepted();

    response = responseBuilder.build();

    // Rule for Client...
    Mockito.when(client.target(uri)).thenReturn(webTarget);

    // Rule for ClientConfiguration...
    Mockito.when(clientConfig.createWebResource(Mockito.anyString())).thenReturn(webTarget);

    // Rules for WebTarget...
    Mockito.when(webTarget.path(Mockito.anyString())).thenReturn(webTarget);
    Mockito.when(webTarget.register(xmlReader.getClass())).thenReturn(webTarget);
    Mockito.when(webTarget.queryParam(Mockito.anyString(), Mockito.anyObject())).thenReturn(webTarget);
    Mockito.when(webTarget.request()).thenReturn(invocationBuilder);

    // Rules for Invocation.Builder...
    Mockito.when(invocationBuilder.header(Mockito.anyString(), Mockito.anyObject())).thenReturn(invocationBuilder);
    Mockito.when(invocationBuilder.accept(Mockito.anyString())).thenReturn(invocationBuilder);
    Mockito.when(invocationBuilder.get(Response.class)).thenReturn(response);

    Mockito.when(response.readEntity(String.class)).thenReturn(content);

//  String entity = response.readEntity(String.class);
    response.close();

} // end of constructor...

public ClientConfiguration getClientConfiguration() {
    return clientConfig;
} // end of method...

如果有人可以帮助我如何根据不同的内容类型读取实体.

If someone could help me how to read the entity based on different content types.

谢谢

推荐答案

像这样的测试非常脆弱,因为它们的级别太低了……在这种情况下,您不需要声明很多东西,因此您测试不够好.通常最好不要重复在客户端中必须进行的呼叫,而要模拟要访问的资源.

Tests like this are extremely brittle, as they are too low-level... and in this case you don't assert a lot of things, so you're not testing good enough. It's generally better not to repeat the calls you'll have to make in your client, but instead mock the resource you want to access.

一种简单的方法是使用DropwizardClientRule.

An easy way to do so, is to use a DropwizardClientRule. There's an example in the tests.

这篇关于使用Mockito的模拟球衣客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆