首页 文章

问题与Spring 4 Jersey 2.8 Spock组合

提问于
浏览
0

我真的很难让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
    }
}

这是我的 Jersey 资源类

@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

1 回答

  • 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
        }
    }
    

相关问题