使用jersey-spring3从JerseyTest容器中检索托管bean [英] Retrieve a managed bean from a JerseyTest container with jersey-spring3

查看:112
本文介绍了使用jersey-spring3从JerseyTest容器中检索托管bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是上一个问题指定自定义应用程序上下文的后续问题。

This question is a follow on from a previous question Specify Custom Application Context.

我们正在使用jersey-spring将泽西1.x的一些数据服务迁移到泽西2.x.使用jersey-spring3。

We are migrating some of our data services from Jersey 1.x using jersey-spring to Jersey 2.x using jersey-spring3.

我们有几个继承自JerseyTest的测试类。其中一些类使用未在web.xml文件中指定的自定义applicationContext.xml文件。

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.

对于对象模拟目的,我们将在Jersey资源中模拟一些组件。

For object mocking purposes we would mock out some components in our Jersey Resources.

在Jersey 1.x中,我们可以通过

In Jersey 1.x we could mock objects in the application context file by

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

并按如下方式检索这些模拟实例

and retrieve these mocked instances as follows

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

泽西2.x如何使用jersey-spring3实现同样的目标?

How can the same be achieved with Jersey 2.x using jersey-spring3?

我已经梳理了 API文档用户指南和一些来源但无法找到答案。

I have combed through the API docs, user guides and some of the sources but was unable to find an answer.

谢谢。

编辑:

我们将使用JAX中的模拟bean- RS资源。我们的资源中有 @Autowired 的服务接口。

We will be using the mocked beans inside of our JAX-RS resources. We have service interfaces that are @Autowired into our resources.

例如

@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 mock out the and set the expectations on these services.

例如

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


推荐答案

注意:我不是Spring专家,我认为这是一种解决方案而非推荐方法。希望有人会提出更好的解决方案。

Note: I am not a Spring expert and I consider this to be rather a work-around than a recommended approach. Hopefully someone will come up with a better solution.

您无法获得 ApplicationContext 实例通过调用 ContextLoader#getCurrentWebApplicationContext(),因为在使用Jersey Test Framework时,默认情况下,Jersey 2.x运行时在Servlet容器外部初始化( JerseyTest )用于单位/ e2e测试。

You can't obtain an ApplicationContext instance by calling ContextLoader#getCurrentWebApplicationContext() because Jersey 2.x runtime is by default initialized outside of a Servlet container when using Jersey Test Framework (JerseyTest) for unit/e2e tests.

在这种情况下,您需要使用一些解决方法来获取 ApplicationContext 通过实施 ApplicationContextAware 测试包中的接口:

In this case you need to use a little work-around to obtain an ApplicationContext by implementing an ApplicationContextAware interface in your 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" />
...

您可以在测试中使用它:

And you can use it in your 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());
    }
}

注意:如果你想要将您的应用程序部署到Servlet容器并对其运行( JerseyTest )测试,请参阅 Jersey测试框架章节(特别是外部容器部分。)

Note: If you want to deploy your application to a Servlet container and run your (JerseyTest) tests against it, consult Jersey Test Framework chapter in the Users Guide (especially External container section).

这篇关于使用jersey-spring3从JerseyTest容器中检索托管bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆