Java 在jersey资源中模拟Springbean
我在Jerse资源和spring互动中使用@Autowire。现在我想能够使用mock 自动连线字段上的对象 我能在泽西1.17版上做到这一点,但在2.2版上我找不到任何文档 我已经厌倦了这样的事情 并在设置容器时提供了自己的SpringComponent,但这两种方法都可以Java 在jersey资源中模拟Springbean,java,spring,rest,mocking,jersey,Java,Spring,Rest,Mocking,Jersey,我在Jerse资源和spring互动中使用@Autowire。现在我想能够使用mock 自动连线字段上的对象 我能在泽西1.17版上做到这一点,但在2.2版上我找不到任何文档 我已经厌倦了这样的事情 并在设置容器时提供了自己的SpringComponent,但这两种方法都可以 Example: @Path("/myresource") public class MyResource { @Autowired private ConfigurationApi configurationA
Example:
@Path("/myresource")
public class MyResource {
@Autowired
private ConfigurationApi configurationApi;
@GET
@Produces("text/plain")
public String getIt() {
boolean isTrue = configurationApi.isEnabled("aconfig");
return "Hi there!: configuration is " + isTrue;
}
在泽西1.17中,我可以做这样的事情:
return new WebAppDescriptor.Builder("rest.resource")
.contextPath("application")
.servletClass(SpringServlet.class)
.servletPath("api")
.contextParam("contextClass", "rest.resource.test.spring.MockableContext")
.contextParam(SpringServlet.CONTEXT_CONFIG_LOCATION, "context.poc-test.xml")
.contextListenerClass(ContextLoaderListener.class)
.build();
然后在以后的实际测试中,我可以通过哈希映射将模拟对象添加到上下文类中
欢迎提供任何帮助。,如何加载Spring上下文?你使用SpringJUnit4ClassRunner吗?如果是,您可以在SpringXML文件中为ConfigurationApi定义模拟bean。