我很难让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
答案 0 :(得分:1)
最后,我在修复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
}
}