Java – use jersey-spring3 to retrieve managed beans from the Jersey test container

This question is derived from the previous question specify custom application context

We are using Jersey spring to bring Jersey 1 Some data services of X are migrated from Jersey spring to Jersey 2 x.

We have several test classes that inherit from jerseytest Some of these classes are not used in web The custom ApplicationContext specified in the XML file XML file

In order to ridicule the target, we will ridicule some components of our Jersey resources section

In Jersey 1 X we can simulate objects in the application context file

<bean id="mockBean" class="org.easymock.EasyMock" 
    factory-method="createStrictMock" autowire="byName">
    <constructor-arg index="0" value="com.xxx.xxx.ClassToMock" /> 
</bean>

And retrieve examples of these mockery as follows

ClassToMock obj = (ClassToMock)ContextLoader
    .getCurrentWebApplicationContext()
    .getAutowireCapablebeanfactory()
    .getBean("mockBean");

How to use Jersey – spring 3?

I have combed API docs, user guides and some sources, but I can't find the answer

thank you.

Edit:

We will use the mocking bean in the Jax - RS resource We have @ Autowired service interface to our resources

for example

@Path(ProductResource.RESOURCE_PATH)
@Component
@Scope("prototype")
public class ProductResource 
extends GenericResource<Product,BaseModel> {

    /*
     * Members
     */

    public static final String RESOURCE_PATH = "product/";

    @Autowired
    protected ProductService productService;

    ...

We want to laugh at the expectations of these services

for example

<bean id="productService" class="org.easymock.EasyMock" 
    factory-method="createStrictMock">
    <constructor-arg index="0" 
        value="com.xxx.xxx.service.ProductService" /> 
</bean>

Solution

Note: I am not a spring expert. I think this is a more effective method than the recommended method I hope someone will come up with a better solution

You cannot get an ApplicationContext instance by calling contextloader #getcurrentwebapplicationcontext(), because when using the Jersey test framework (Jersey test) for unit / E2E testing, Jersey 2 is used by default outside the servlet container X runtime

In this case, you need to use some solutions to obtain ApplicationContext by implementing an applicationcontextaware interface in the test package:

public class ApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtils.applicationContext = applicationContext;
    }
}

Once you have this class, don't forget to mention it in your application context descriptor:

...
<bean class="org.glassfish.jersey.examples.helloworld.spring.ApplicationContextUtils" />
...

You can use it in tests:

public class JerseySpringResourceTest extends JerseyTest {

    // ... Configure ...

    @Before
    public void mockUp() throws Exception {
        // ApplicationContext is ready in your @Before methods ...
        assertThat(ApplicationContextUtils.getApplicationContext(),notNullValue());
    }

    @Test
    public void testJerseyResource() {
        // ... as well as in your test methods.
        assertThat(ApplicationContextUtils.getApplicationContext(),notNullValue());
    }
}

Note: if you want to deploy the application to the servlet container and perform (Jersey test) tests on it, please refer to the Jersey test framework chapter in the user's Guide (especially the external container section)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>