首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试jersey 2客户端和webtarget

测试jersey 2客户端和webtarget
EN

Stack Overflow用户
提问于 2015-11-30 18:56:43
回答 1查看 11.9K关注 0票数 3

我用的是jersey 2

我有一个抽象类,用来构造我的请求。现在,我还有一些抽象的客户机类,我将它们用作代理类和实际实现。这些方法工作得很好,但还没有经过测试。

我的问题是,我如何才能测试它,而不必运行它所连接的run服务?

代码语言:javascript
复制
public abstract class AbstractRestProxy {

private Client client;
private WebTarget service;

/**
 * Get the base {@link Client} and {@link WebTarget}
 */
@PostConstruct
public void base() {
    this.client = ClientBuilder.newClient();
    this.service = this.client.target(this.getBaseUri());
}

/**
 * close the connection before destroy
 */
@PreDestroy
protected void close() {
    if (this.client != null) {
        this.client.close();
    }
}

/**
 *
 * @return get the basePath
 */
protected abstract String getBasePath();

/**
 *
 * @return get the baseUri
 */
protected abstract String getBaseUri();

/**
 *
 * @param paths
 *            the paths to get for the rest service
 * @return {@link javax.ws.rs.client.Invocation.Builder}
 */
protected Builder getRequest(final String... paths) {
    WebTarget serviceWithPath = this.getServiceWithPaths();
    for (final String path : paths) {
        serviceWithPath = serviceWithPath.path(path);
    }
    return serviceWithPath.request(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON);
}

例如,我用来获取带有标识符的响应的方法,我使用这个方法。

代码语言:javascript
复制
public Response getByID(final ID identifier) {
    return this.getRequest(identifier.toString()).get();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-07 17:19:02

我发现这样做效果很好。因为我甚至嘲笑了响应,所以我不认为我必须关闭响应。

代码语言:javascript
复制
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClientBuilder.class)
public class AbstractGenericRestActiveProxyTest {

final Client mockClient = Mockito.mock(Client.class);
final Response mockResponse = Mockito.mock(Response.class);

private final AbstractRestProxy proxy = new AbstractRestProxy();

@Before
public void start() {
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);

    final Builder mockBuilder = Mockito.mock(Builder.class);
    Mockito.when(mockBuilder.get()).thenReturn(this.mockResponse);
    Mockito.when(mockBuilder.post(Matchers.any())).thenReturn(this.mockResponse);
    Mockito.when(mockBuilder.put(Matchers.any())).thenReturn(this.mockResponse);
    Mockito.when(mockBuilder.delete()).thenReturn(this.mockResponse);

    final WebTarget mockWebTarget = Mockito.mock(WebTarget.class);
    Mockito.when(mockWebTarget.path(Matchers.anyString())).thenReturn(mockWebTarget);
    Mockito.when(mockWebTarget.request(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);

    Mockito.when(this.mockClient.target(Matchers.anyString())).thenReturn(mockWebTarget);

    PowerMockito.mockStatic(ClientBuilder.class);
    PowerMockito.when(ClientBuilder.newClient()).thenReturn(this.mockClient);
    this.proxy.base();
}

@After
public void stop() {
    this.proxy.close();
}

@Test
public void testGetByID() {
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);

    final Response result = this.proxy.getByID(1);
    Assert.assertEquals(200, result.getStatus());
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33996983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档