title: Jersey - Unit testing resource with spring-injected service
body: <p>My question is similar to <a href="https://stackoverflow.com/questions/28614846/how-to-in-memory-unit-test-spring-jersey">this one</a>.</p>
  
  <p><strong><em>Setting</em></strong>:
  I have a <code>GreetingResource</code> which has a <code>Autowired</code> <code>GreetingService</code> to handle <code>GET</code> requests:</p>
  
  <pre><code>@Path("greetings")
  public class GreetingResource 
  {
  
  @Autowired
  private GreetingService service;
  
  @GET
  @Produces(MediaType.TEXT_PLAIN_VALUE)
  public String getHello()
  {
      try
      {
          return service.greet("world");
      }
      catch(SQLException e)
      {
          System.out.println("Request failed (500)");
          e.printStackTrace();
          return null;
      }
  }
  }
  </code></pre>
  
  <p><strong><em>Problem</em></strong>: In my unit test I get
      org.springframework.beans.factory.NoSuchBeanDefinitionException: </p>
  
  <pre><code>No qualifying bean of type 'demo.rest.jersey.spring.GreetingService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1466)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1097)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1059)
      at org.glassfish.jersey.server.spring.AutowiredInjectResolver.getBeanFromSpringContext(AutowiredInjectResolver.java:104)
      at org.glassfish.jersey.server.spring.AutowiredInjectResolver.resolve(AutowiredInjectResolver.java:96)
  </code></pre>
  
  <p><strong><em>Unit test class</em></strong>: In order to unit test this I have a <code>testContext.xml</code> which acts as application context for unit tests. It creates just one bean, namely the <code>TestConfig</code>:</p>
  
  <pre><code>@Configuration
  public class TestConfig 
  {
      @Bean
      @Primary
      public GreetingService greetingService() throws SQLException
      {
          GreetingService service = Mockito.mock(GreetingService.class);
          when(service.greet(anyString())).thenReturn("Hello");
          return service;
      }
  }
  </code></pre>
  
  <p>The <code>testContext.xml</code> should be loaded by the unit test and hence the correct beans created:</p>
  
  <pre><code>public class Test_GreetingResource extends JerseyTest 
  {
      @Override
      protected TestContainerFactory getTestContainerFactory() 
      {
          return new GrizzlyWebTestContainerFactory();
      }
  
      @Override
      protected DeploymentContext configureDeployment(){
          return ServletDeploymentContext
                  .forServlet(new ServletContainer(new TestApp()))
                  .addListener(ContextLoaderListener.class)
                  .contextParam("contextConfigLocation", "classpath:testContext.xml")
                  .build();
      }
  
      @Test
      public void doTest() throws SQLException
      {
          String response = target("greetings").request().get(String.class);
          assertEquals("response incorrect", "Hello", response);
          System.out.println(response);
      }
  }
  </code></pre>
  
  <p><strong><em>Notes</em></strong>: The <code>TestApp</code> simply registers the <code>GreetingResource</code></p>
  
  <pre><code>public class TestApp extends ResourceConfig 
  {
      public TestApp()
      {
          register(GreetingResource.class);
          packages("demo.rest.jersey.spring");
      }
  }
  </code></pre>
  
  <p>Here is also the <code>testContext.xml</code> that I mentioned above:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
      &lt;bean id="testConfig" class="demo.rest.jersey.spring.TestConfig"&gt;&lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong><em>Additional question</em></strong>: If the <code>GreetingService</code> mock is created as a bean, can I somehow alter the mock behaviour? I.e. right now I specify in the <code>TestConfig</code> what response the mock should give but I might want to alter that behaviour from test to test.</p>
  
  <p>Thanks for the help!</p>