3

I am using jersey 2

I have an abstract class with which I construct my request. Now, I also have some abstract client classes I use as proxy classes and actual implementations. These work well, but are untested.

My question is how I could test this, without having to run the webservice which it is connecting to?

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);
}

A method I use to, for example get a response with an identifier, I use this method.

public Response getByID(final ID identifier) {
    return this.getRequest(identifier.toString()).get();
}
bobK
  • 704
  • 2
  • 7
  • 24
  • If you test any direct implementation of this "base client" you are testing the "base client" as well. I suggest using things like Wiremock to test your client without any need to start up the full service. Also remember to ALWAYS call response.close() on JAX-RS response or you will be leaking resources (for example HTTP Connections). – Rafal G. Dec 07 '15 at 07:49

1 Answers1

10

I found that this works well. Since I am mocking even the response, I don't think I have to close the response.

@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());
}
bobK
  • 704
  • 2
  • 7
  • 24
  • Is it possible without PowerMock? The project I need to unit test right now uses WebTarget and it's JUnit 5/Jupiter, and we CAN'T change this dependency. I'm thinking about creating a flag and by-passing this WebTarget thing, e.g. private boolean isTesting; and a setter. – Rasshu Apr 03 '20 at 14:26
  • Well, you have to mock static methods in my answer from back then. If you don't have to, you shouldn't need powermock – bobK Apr 06 '20 at 08:54