title: How to in-memory unit test Spring-Jersey
body: <p>I'm working with Spring-Jersey3 and cannot figure out how to unit test the RESTFul API with Spring beans</p>
  
  <p>Controller </p>
  
  <pre><code>package com.controller;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import com.service.DataSource;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  @Path("test")
  @Component
  public class SpringController {
      @Autowired
      private DataSource datasource;
  
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String getHello() {
          return new String(datasource.load());
      }
  }
  </code></pre>
  
  <p>Service Interface</p>
  
  <pre><code>package com.service;
  
  public interface DataSource {
      public String load();
  }
  </code></pre>
  
  <p>Service Implementation</p>
  
  <pre><code>package com.service;
  
  import org.springframework.stereotype.Repository;
  
  @Repository
  public class DataSourceImpl implements DataSource {
  
      @Override
      public String load() {
          return "Hello";
      }
  }
  </code></pre>
  
  <p>ResourceRegister.java (Jersey resource register)</p>
  
  <pre><code>package com.component;
  
  import org.glassfish.jersey.server.ResourceConfig;
  import com.controller.SpringController;
  
  public class ResourceRegister extends ResourceConfig {
  
      public ResourceRegister () {
          register(SpringController.class);
      }
  }
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"&gt;
  
  &lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  
  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;classpath:applicationContext.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;servlet&gt;
    &lt;servlet-name&gt;Jersey&lt;/servlet-name&gt;
    &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
  &lt;init-param&gt;
    &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
    &lt;param-value&gt;com.component.ResourceRegister&lt;/param-value&gt;
  &lt;/init-param&gt;
  &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;Jersey&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>serviceContext.xml (Application Context)</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"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  &lt;context:component-scan base-package="com.service" /&gt;
  &lt;context:component-scan base-package="com.controller" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Unit test &lt;&lt;-- I really have no Idea how to test this</p>
  
  <pre><code>public class test extends JerseyTest {
      public test() {
          super("com.service", "com.controller");
      }
  
      @Override
      protected AppDescriptor configure() {
          return new WebAppDescriptor.Builder("com.service","com.controller")
                 .contextParam("contextConfigLocation", "classpath:serviceContext.xml")
                 .contextPath("/rest")
                 .servletClass("org.glassfish.jersey.servlet.ServletContainer.class")
                 .initParam("javax.ws.rs.Application", "com.component.ResourceRegister")
                 .build();
      }
  
      @Test
      public void test() {
          Client client = new Client();
          WebResource resource = client.resource("test");
  
          ClientResponse response = resource.post(ClientResponse.class);
  
          assertEquals(200, resposne.getStatus());
      }
  }
  </code></pre>
  
  <p>Project <a href="https://github.com/Mrshll187/SpringJersey" rel="noreferrer">Source Code</a></p>
  
  <p><strong>Problem : Dependency injection returns null</strong></p>