title: Access springbeans in JerseyTest with Jersey2.0
body: <p>I have jersey project with spring. Now My test is deriverd from JerseyTest. When I try to do</p>
  
  <pre><code>@AutoWired 
  RestTemplate restTemplate;
  </code></pre>
  
  <p>It looks like spring is not working in jersey test. I did some research and found some links like
  <a href="http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/" rel="nofollow noreferrer">spring_jersey</a>
  but it did not work ,since I am using jersey2.0.</p>
  
  <p>My code looks like</p>
  
  <pre><code> //AbstractTest 
      package com.test;
  
  
              import javax.ws.rs.client.WebTarget;
              import javax.ws.rs.core.Application;
  
          import org.glassfish.jersey.client.ClientConfig;
          import org.glassfish.jersey.filter.LoggingFilter;
          import org.glassfish.jersey.jackson.JacksonFeature;
          import org.glassfish.jersey.server.ResourceConfig;
          import org.glassfish.jersey.test.JerseyTest;
          import org.glassfish.jersey.server.ServerProperties;
          import org.glassfish.jersey.server.validation.ValidationFeature;
  
          public abstract class AbstractTest extends JerseyTest
          {
              protected WebTarget getRootTarget(final String rootResource)
              {
                  return client().target(getBaseUri()).path(rootResource);
              }
  
              @Override
              protected final Application configure()
              {
                  final ResourceConfig application = configureApplication();
  
                  // needed for json serialization
                  application.register(JacksonFeature.class);
  
                  // bean validation
                  application.register(ValidationFeature.class);
  
                  // configure spring context
                  application.property("contextConfigLocation", "classpath:/META-INF/applicationContext.xml");
  
                  // disable bean validation for tests
                  application.property(ServerProperties.BV_FEATURE_DISABLE, "true");
  
                  return application;
              }
  
              protected abstract ResourceConfig configureApplication();
  
              @Override
              protected void configureClient(final ClientConfig config)
              {
                  // needed for json serialization
                  config.register(JacksonFeature.class);
  
                  config.register(new LoggingFilter(java.util.logging.Logger.getLogger(AbstractResourceTest.class.getName()), false));
  
                  super.configureClient(config);
              }
          }
  
  
  
      package com.test;
  
      import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
      import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
      import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
  
      //MyTest
          import java.io.FileNotFoundException;
          import java.io.FileReader;
          import java.io.IOException;
  
      import javax.ws.rs.client.WebTarget;
      import javax.ws.rs.core.Response;
  
      import org.apache.commons.io.IOUtils;
      import org.glassfish.jersey.server.ResourceConfig;
      import org.junit.Before;
      import org.junit.Test;
      import org.springframework.http.HttpMethod;
      import org.springframework.http.MediaType;
      import org.springframework.test.web.client.MockRestServiceServer;
      import org.springframework.test.web.client.match.MockRestRequestMatchers;
      import org.springframework.web.client.RestTemplate;
  
      import junit.framework.Assert;
  
      public final class MyTest extends AbstractTest
          {
  
          private static final String ROOT_RESOURCE_PATH = "/testUrl";
  
          @AutoWired
  private RestTemplate restTemplate;
          private MockRestServiceServer mockServer;
  
  
          @Before
          public void setup(){
              this.restTemplate = new RestTemplate();
              this.mockServer = MockRestServiceServer.createServer(restTemplate);
          }
  
          @Test
          public void testPostWithString() {
  
              WebTarget target = getRootTarget(ROOT_RESOURCE_PATH).path("");
              String entityBody = new String();
              entityBody = " My test data";
  
  
              final javax.ws.rs.client.Entity&lt;String&gt; entity = javax.ws.rs.client.Entity.entity(entityBody, "text/plain");
  
  
              mockServer.expect(MockRestRequestMatchers.requestTo(ROOT_RESOURCE_PATH)).andExpect(method(HttpMethod.POST)).andExpect(content().string(entityBody))
                      .andRespond(withSuccess("resultSuccess", MediaType.TEXT_PLAIN));
  
  
              final Response response = target.request().post(entity);
              Assert.assertNotNull("Response must not be null", response.getEntity());
              Assert.assertEquals("Response does not have expected response code", 200, response.getStatus());
  
              System.out.println("Response = " + response.getEntity());
  
              String data = response.readEntity(String.class);
  
              System.out.println("Response = " + data);
              if(response.ok() != null)
              {
                  System.out.println("Ok");
              }
          }
      }
  </code></pre>
  
  <p>Update:</p>
  
  <pre><code>public class SimpleJerseyTest extends ApplicationContextAwareJerseyTest {
      private static final String ROOT_RESOURCE_PATH = "/test";
  
      @Override
      public void configureApplication(ResourceConfig config) {
          config.register(MyApp.class);
          config.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
      }
  
      @Before
      public void setUp() {
          try{
              ((ConfigurableApplicationContext)this.applicationContext).refresh();
              super.setUp();
          }catch(Exception e){
  
          }
      this.mockServer = MockRestServiceServer.createServer(restTemplate);
      }
  
      @Autowired
      private RestTemplate restTemplate;
  
      private MockRestServiceServer mockServer;
  
      @Test
      public void doitOnce() {
          WebTarget target = target(ROOT_RESOURCE_PATH);
  
          String entityBody = new String();
  
          final javax.ws.rs.client.Entity&lt;String&gt; entity = javax.ws.rs.client.Entity.entity(entityBody, "text/plain");
  
  
          mockServer.expect(MockRestRequestMatchers.requestTo(ROOT_RESOURCE_PATH)).andExpect(method(HttpMethod.POST)).andExpect(content().string(entityBody))
                  .andRespond(withSuccess("resultSuccess", MediaType.TEXT_PLAIN));
  
  
          final Response response = target.request().post(entity);
  
  
          System.out.println("Response = " + response.getEntity());
  
          String data = response.readEntity(String.class);
  
          System.out.println("Response = " + data);
          if(response.ok() != null)
          {
              System.out.println("Ok");
          }
      }
  }
  </code></pre>
  
  <p>I have added bean in </p>
  
  <blockquote>
    <p>src/test/resources/META-INF/applicationContext.xml</p>
  </blockquote>
  
  <pre><code>&lt;!-- Our REST Web Service client --&gt;
  &lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/&gt;
  </code></pre>
  
  <p>Same bean I have added in </p>
  
  <blockquote>
    <p>src/main/resources/META-INF/applicationContext.xml</p>
  </blockquote>
  
  <pre><code>!-- Our REST Web Service client --&gt;
  &lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/&gt;
  </code></pre>