我真的很难让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 moreI'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 } }更多推荐
发布评论