使用 jersey-spring3 从 JerseyTest 容器中检索托管 bean

2024-01-21

这个问题是上一个问题的后续问题指定自定义应用程序上下文 https://stackoverflow.com/questions/18278769/specify-custom-application-context.

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

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

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

在 Jersey 1.x 中,我们可以通过以下方式模拟应用程序上下文文件中的对象

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

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

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

如何使用 jersey-spring3 在 Jersey 2.x 中实现同样的效果?

我梳理了API docs https://jersey.java.net/documentation/latest/index.html, 用户指南 https://jersey.java.net/apidocs/latest/jersey/index.html和一些sources https://github.com/jersey/jersey但无法找到答案。

谢谢。

EDIT:

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

e.g.

@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;

    ...

我们想要模拟这些服务并设定对这些服务的期望。

e.g.

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

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

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

在这种情况下,您需要使用一些解决方法来获得ApplicationContext通过实施应用上下文感知 http://static.springsource.org/spring/docs/3.2.0.BUILD-SNAPSHOT/api/org/springframework/context/ApplicationContextAware.html测试包中的接口:

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;
    }
}

一旦你有了这个类,不要忘记在你的应用程序上下文描述符中提及它:

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

您可以在测试中使用它:

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:如果您想将应用程序部署到 Servlet 容器并运行您的 (JerseyTest)对其进行测试,请咨询泽西岛测试框架 https://jersey.java.net/documentation/latest/test-framework.html用户指南中的章节(特别是外部容器 https://jersey.java.net/documentation/latest/test-framework.html#d0e11286部分)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jersey-spring3 从 JerseyTest 容器中检索托管 bean 的相关文章

随机推荐