这个问题是上一个问题的后续问题指定自定义应用程序上下文 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>