Spring 4 + Jersey 2.8 + Spock组合问题(Issue with Spring 4 + Jersey 2.8 + Spock combination)

编程入门 行业动态 更新时间:2024-10-26 11:29:09
Spring 4 + Jersey 2.8 + Spock组合问题(Issue with Spring 4 + Jersey 2.8 + Spock combination)

我真的很难让Spring 4 + Jersey 2.8 + Spock测试一起工作。 我正在尝试为通过Spring连接的Jersey REST服务编写一个Spock单元测试。

我正在使用spock-spring-0.7-groovy-2.0注释来注释我的Spock测试,如下所示。

TestAppConfig是我的Spring 4配置文件。 出于某种原因,它不是使用这个Java Spring配置类,而是在类路径中查找applictionContext.xml。 Stacktrace可以在下面找到。

我在这里想念的是什么? 有没有人有这个组合工作?

@ContextConfiguration(classes = [TestAppConfig.class]) class WebServiceTest extends Specification { @Shared protected HttpServer server @Shared protected WebTarget target void setupSpec() { final ResourceConfig rc = new ResourceConfig().packages(["org.myapp.webservice.rest.controller"]) .property("contextClass", "org.springframework.web.context.support.AnnotationConfigApplicationContext") server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:"), rc) server.start(); def config = new ClientConfig() Client c = ClientBuilder.newClient(config) target = c.target(baseUri); } void cleanupSpec() { server?.shutdownNow() } def "Query all clients"(){ when: String responseMsg = target.path("/clients").request().get(String.class) then: responseMsg != null } }

这是我的泽西岛资源类

@Component @Path("/") public class ClientHierarchyResource { private static final Logger LOGGER = LoggerFactory.getLogger(ClientHierarchyResource.class); @Autowired private IClientHierarchyService clientHierarchyService; ... }

这是我得到的例外:

20:14:26.112 [main] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org.myapp. application.common.springconfig.TestAppConfig] org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [ applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303) Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329) ... 28 more

I'm having really tough time getting Spring 4 + Jersey 2.8 + Spock test working together. I'm trying to a write a Spock unit test for Jersey REST service wired through Spring.

I'm annotating my Spock test using spock-spring-0.7-groovy-2.0 annotations as shown below.

TestAppConfig is my Spring 4 configuration file. For some reason, instead of using this Java Spring configuration class, it is looking applictionContext.xml in the classpath. Stacktrace can be found below.

What am I missing here? Has anyone got this combination working?

@ContextConfiguration(classes = [TestAppConfig.class]) class WebServiceTest extends Specification { @Shared protected HttpServer server @Shared protected WebTarget target void setupSpec() { final ResourceConfig rc = new ResourceConfig().packages(["org.myapp.webservice.rest.controller"]) .property("contextClass", "org.springframework.web.context.support.AnnotationConfigApplicationContext") server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:"), rc) server.start(); def config = new ClientConfig() Client c = ClientBuilder.newClient(config) target = c.target(baseUri); } void cleanupSpec() { server?.shutdownNow() } def "Query all clients"(){ when: String responseMsg = target.path("/clients").request().get(String.class) then: responseMsg != null } }

This is my Jersey resource class

@Component @Path("/") public class ClientHierarchyResource { private static final Logger LOGGER = LoggerFactory.getLogger(ClientHierarchyResource.class); @Autowired private IClientHierarchyService clientHierarchyService; ... }

Here is the exception I get:

20:14:26.112 [main] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org.myapp. application.common.springconfig.TestAppConfig] org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [ applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303) Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329) ... 28 more

最满意答案

最后,我在修复ResourceConfig配置后得到了它。

@ContextConfiguration(classes = [TestAppConfig.class]) class WebServiceTest extends Specification { @Shared protected HttpServer server @Shared protected WebTarget target void setupSpec() { final ResourceConfig rc = new ResourceConfig().packages(PACKAGES).property("contextConfig", new AnnotationConfigApplicationContext(TestAppConfig.class)) server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:"), rc) server.start(); def config = new ClientConfig() Client c = ClientBuilder.newClient(config) target = c.target(baseUri); } void cleanupSpec() { server?.shutdownNow() } def "Query all clients"(){ when: String responseMsg = target.path("/clients").request().get(String.class) then: responseMsg != null } }

Finally I got it working after fixing the ResourceConfig configuration.

@ContextConfiguration(classes = [TestAppConfig.class]) class WebServiceTest extends Specification { @Shared protected HttpServer server @Shared protected WebTarget target void setupSpec() { final ResourceConfig rc = new ResourceConfig().packages(PACKAGES).property("contextConfig", new AnnotationConfigApplicationContext(TestAppConfig.class)) server = GrizzlyHttpServerFactory.createHttpServer(URI.create("http://localhost:"), rc) server.start(); def config = new ClientConfig() Client c = ClientBuilder.newClient(config) target = c.target(baseUri); } void cleanupSpec() { server?.shutdownNow() } def "Query all clients"(){ when: String responseMsg = target.path("/clients").request().get(String.class) then: responseMsg != null } }

更多推荐

本文发布于:2023-08-03 15:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   Spring   Jersey   combination   Issue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!