REST Test – Unit Tests and Coverage Reporting

Now we start to get to the crux of this “Hello World” example. First – the unit tests and later the integration tests.

I’ve added the JUnit and Mockito dependencies to pom.xml:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.9.5</version>
  <scope>test</scope>
</dependency>

and the surefire plugin. Notice the -XX:-UseSplitVerifier argument. This was required because I’m using Java 7.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.15</version>
  <configuration>
    <argLine>-XX:-UseSplitVerifier</argLine>
  </configuration>
</plugin>

Also, as part of personal preferences, I added the Cobertura coverage reporting plugin to pom.xml:

<reporting>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <version>2.5.2</version>
    </plugin>
  </plugins>
</reporting>

I’ll skip the unit test for the AnEntity class as it’s trivial. The AnEntityFacadeRESTTest class however is way more interesting:

package com.nigeleke.restin.service;

import com.nigeleke.restin.model.AnEntity;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.RuntimeDelegate;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;

@RunWith(MockitoJUnitRunner.class)
public class AnEntityFacadeRESTTest {

  @Before
  public void setUp() {
    RuntimeDelegate.setInstance(runtimeDelegate);
  }

  @Test
  public void testCreate() throws Exception {
    AnEntity anEntity = mock(AnEntity.class);
    UriBuilder uriBuilder = mock(UriBuilder.class);
    URI uri = URI.create("http://restin.local/rest/anentity/");
    ResponseBuilder responseBuilder = mock(ResponseBuilder.class);
    Response response = mock(Response.class);

    when((uriInfo).getRequestUriBuilder()).thenReturn(uriBuilder);
    when((uriBuilder).path(isA(String.class))).thenReturn(uriBuilder);
    when((uriBuilder).build()).thenReturn(uri);
    when((runtimeDelegate).createResponseBuilder()).thenReturn(responseBuilder);
    when((runtimeDelegate).createUriBuilder()).thenReturn(uriBuilder);
    when((responseBuilder).status(Response.Status.CREATED)).thenReturn(responseBuilder);
    when(Response.status(Response.Status.CREATED)).thenReturn(responseBuilder);
    when((responseBuilder).location(uri)).thenReturn(responseBuilder);
    when((responseBuilder).entity(any(AnEntity.class))).thenReturn(responseBuilder);
    when((responseBuilder).contentLocation(uri)).thenReturn(responseBuilder);
    when((responseBuilder).build()).thenReturn(response);

    Response result = instance.create(anEntity);

    verify(em).persist(anEntity);
    verify(em).flush();

    assertEquals(result, response);
  }

  @Mock
  private EntityManager em;

  @Mock
  private UriInfo uriInfo;

  @Mock
  private RuntimeDelegate runtimeDelegate;

  @InjectMocks
  private AnEntityFacadeREST instance;
}

This is mocked-up-to-the-hilt to mock out the container dependencies. First off we have a RuntimeDelegate mock. This became necessary when the @POST (create) method returned a Response object, rather than void.

RuntimeDelegate returns a ResponseBuilder which, in turn, builds the Response that we require.

I have to admit the test is ug-a-leeeeee, especially as, ultimately, the only thing that gets verified here are that the EntityManager correctly creates the entity.

Other tests, like testEdit, testRemove, testFind etc have a similar requirement to mock out all the dependencies, but create proved the most complex to unit test.

Nevertheless, the unit tests complete quickly and do not place any dependencies on containers or databases.

REST Test - Generate REST Service REST Test - Add Integration Tests

3 thoughts on “REST Test – Unit Tests and Coverage Reporting

  1. Thanks for this, I’m trying to unit test similar code right now. Stuck on how to define javax.ws.rs.core.UriInfo for testing.

    On line 27:
    when((uriInfo).getRequestUriBuilder()).thenReturn(uriBuilder);

    Where is uriInfo defined?

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.