title: Mocking a Jersey REST Api running on Grizzly using Mockito
body: <p>I am having a play with testing a RESTful api which I have set up using Jersey.  I want to test it using JUnit while running it with Grizzly to provide the Http container.  </p>
  
  <p>General testing using the server works ok, i.e. sending requests and receiving responses etc.</p>
  
  <p>The api is called CMSApi and it has a dependency called skyService which I would like to mock out so I'm testing just the api.
  So the question is how can I inject the CMSApi with a mockSkyService object created using Mockito?  The relevant code is below:</p>
  
  <p>Grizzly Server start up:</p>
  
  <pre><code>public static HttpServer startServer() {
      final ResourceConfig rc = new ResourceConfig().packages("com.sky");
      rc.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
      return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
  </code></pre>
  
  <p>}</p>
  
  <p>My JUnit calls the above start method:</p>
  
  <pre><code>@Before
  public void setUpServer() throws Exception {
  
      // start the server and create the client
      server = Main.startServer();
  
      Client client = ClientBuilder.newClient();
      target = client.target(Main.BASE_URI);
  }
  </code></pre>
  
  <p>I run the test using the <code>@RunWith(MockitoJUnitRunner.class)</code>.</p>
  
  <p>Here are the relevant objects in the test:</p>
  
  <pre><code>//System Under Test
  private CMSApi cmsApi;
  
  //Mock
  @Mock private static SkyService mockSkyService;
  </code></pre>
  
  <p>Here is the test method calling:</p>
  
  <pre><code>  @Test
  public void testSaveTile() {
  
      //given
      final Invocation.Builder invocationBuilder = target.path(PATH_TILE).request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
  
      Tile tile = new Tile(8, "LABEL", clientId, new Date(), null);
  
      //when      
      Response response = invocationBuilder.post(Entity.entity(tile, MediaType.APPLICATION_JSON_TYPE));
  
      //then
      assertEquals(Status.OK.getStatusCode(), response.getStatus());
  
  }
  </code></pre>
  
  <p>Maven dependencies</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
  
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
  &lt;groupId&gt;com.sky&lt;/groupId&gt;
  &lt;artifactId&gt;sky-service&lt;/artifactId&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;sky-service&lt;/name&gt;
  
  &lt;dependencyManagement&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  
   &lt;dependencies&gt;
       &lt;dependency&gt; 
          &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
          &lt;/dependency&gt;        
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.test-framework.providers&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-test-framework-provider-bundle&lt;/artifactId&gt;
          &lt;type&gt;pom&lt;/type&gt;
          &lt;scope&gt;test&lt;/scope&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-bean-validation&lt;/artifactId&gt;
          &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.test-framework.providers&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-test-framework-provider-grizzly2&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.sun.jersey&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-json&lt;/artifactId&gt;
          &lt;version&gt;1.16&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.10&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;!-- Dependency for Mockito --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.mockito&lt;/groupId&gt;
          &lt;artifactId&gt;mockito-all&lt;/artifactId&gt;
          &lt;version&gt;1.9.5&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;version&gt;2.5.1&lt;/version&gt;
              &lt;inherited&gt;true&lt;/inherited&gt;
              &lt;configuration&gt;
                  &lt;source&gt;1.7&lt;/source&gt;
                  &lt;target&gt;1.7&lt;/target&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
              &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
              &lt;version&gt;1.2.1&lt;/version&gt;
              &lt;executions&gt;
                  &lt;execution&gt;
                      &lt;goals&gt;
                          &lt;goal&gt;java&lt;/goal&gt;
                      &lt;/goals&gt;
                  &lt;/execution&gt;
              &lt;/executions&gt;
              &lt;configuration&gt;
                  &lt;mainClass&gt;com.sky.Main&lt;/mainClass&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  
  &lt;properties&gt;
      &lt;jersey.version&gt;2.15&lt;/jersey.version&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  &lt;/properties&gt;
  </code></pre>
  
  <p></p>