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

rob*_*ink 9 java spring unit-testing jersey-2.0

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

我们正在使用泽西弹簧3将泽西1.x的一些数据服务从泽西1.x迁移到泽西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>
Run Code Online (Sandbox Code Playgroud)

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

ClassToMock obj = (ClassToMock)ContextLoader
    .getCurrentWebApplicationContext()
    .getAutowireCapableBeanFactory()
    .getBean("mockBean");
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

编辑:

我们将在JAX-RS资源中使用模拟bean.我们的服务接口属于@Autowired我们的资源.

例如

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

    ...
Run Code Online (Sandbox Code Playgroud)

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

例如

<bean id="productService" class="org.easymock.EasyMock" 
    factory-method="createStrictMock">
    <constructor-arg index="0" 
        value="com.xxx.xxx.service.ProductService" /> 
</bean>
Run Code Online (Sandbox Code Playgroud)

Mic*_*dos 8

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

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

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

...
<bean class="org.glassfish.jersey.examples.helloworld.spring.ApplicationContextUtils" />
...
Run Code Online (Sandbox Code Playgroud)

你可以在你的测试中使用它:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:如果要将应用程序部署到Servlet容器并对其运行(JerseyTest)测试,请参阅"用户指南"(特别是外部容器部分)中的Jersey Test Framework一章.