title: Retrieve a managed bean from a JerseyTest container with jersey-spring3
body: <p>This question is a follow on from a previous question <a href="https://stackoverflow.com/questions/18278769/specify-custom-application-context">Specify Custom Application Context</a>.</p>
  
  <p>We are migrating some of our data services from Jersey 1.x using jersey-spring to Jersey 2.x using jersey-spring3.</p>
  
  <p>We have a few test classes that inherit from JerseyTest. Some of these classes use custom applicationContext.xml files that are not specified in the web.xml file.</p>
  
  <p>For object mocking purposes we would mock out some components in our Jersey Resources.</p>
  
  <p>In Jersey 1.x we could mock objects in the application context file by</p>
  
  <pre><code>&lt;bean id="mockBean" class="org.easymock.EasyMock" 
      factory-method="createStrictMock" autowire="byName"&gt;
      &lt;constructor-arg index="0" value="com.xxx.xxx.ClassToMock" /&gt; 
  &lt;/bean&gt;
  </code></pre>
  
  <p>and retrieve these mocked instances as follows</p>
  
  <pre><code>ClassToMock obj = (ClassToMock)ContextLoader
      .getCurrentWebApplicationContext()
      .getAutowireCapableBeanFactory()
      .getBean("mockBean");
  </code></pre>
  
  <p>How can the same be achieved with Jersey 2.x using jersey-spring3?</p>
  
  <p>I have combed through the <a href="https://jersey.java.net/documentation/latest/index.html" rel="nofollow noreferrer">API docs</a>, <a href="https://jersey.java.net/apidocs/latest/jersey/index.html" rel="nofollow noreferrer">user guides</a> and some of the <a href="https://github.com/jersey/jersey" rel="nofollow noreferrer">sources</a> but was unable to find an answer.</p>
  
  <p>Thank you.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>We will be using the mocked beans inside of our JAX-RS resources. We have service interfaces that are <code>@Autowired</code> into our resources.</p>
  
  <p>e.g.</p>
  
  <pre><code>@Path(ProductResource.RESOURCE_PATH)
  @Component
  @Scope("prototype")
  public class ProductResource 
  extends GenericResource&lt;Product, BaseModel&gt; {
  
      /*
       * Members
       */
  
      public static final String RESOURCE_PATH = "product/";
  
      @Autowired
      protected ProductService productService;
  
      ...
  </code></pre>
  
  <p>We want to mock out the and set the expectations on these services.</p>
  
  <p>e.g.</p>
  
  <pre><code>&lt;bean id="productService" class="org.easymock.EasyMock" 
      factory-method="createStrictMock"&gt;
      &lt;constructor-arg index="0" 
          value="com.xxx.xxx.service.ProductService" /&gt; 
  &lt;/bean&gt;
  </code></pre>