测试球衣 2 客户端和 webtarget
test jersey 2 client and webtarget
我正在使用球衣 2
我有一个摘要 class 用来构造我的请求。
现在,我还有一些抽象客户端 classes 用作代理 classes 和实际实现。这些效果很好,但未经测试。
我的问题是如何测试它,而不必 运行 它连接到的网络服务?
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);
}
我使用的一种方法,例如获取带有标识符的响应,我使用这种方法。
public Response getByID(final ID identifier) {
return this.getRequest(identifier.toString()).get();
}
我发现这很管用。因为我连回复都在嘲笑,所以我觉得我不必关闭回复。
@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());
}
我正在使用球衣 2
我有一个摘要 class 用来构造我的请求。 现在,我还有一些抽象客户端 classes 用作代理 classes 和实际实现。这些效果很好,但未经测试。
我的问题是如何测试它,而不必 运行 它连接到的网络服务?
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);
}
我使用的一种方法,例如获取带有标识符的响应,我使用这种方法。
public Response getByID(final ID identifier) {
return this.getRequest(identifier.toString()).get();
}
我发现这很管用。因为我连回复都在嘲笑,所以我觉得我不必关闭回复。
@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());
}