Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在jersey资源中模拟Springbean_Java_Spring_Rest_Mocking_Jersey - Fatal编程技术网

Java 在jersey资源中模拟Springbean

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

我在Jerse资源和spring互动中使用@Autowire。现在我想能够使用mock 自动连线字段上的对象

我能在泽西1.17版上做到这一点,但在2.2版上我找不到任何文档

我已经厌倦了这样的事情

并在设置容器时提供了自己的SpringComponent,但这两种方法都可以

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。