不让 Spring 干扰服务的纯 JerseyTest

Pure JerseyTest without letting Spring messing with services

我在使用 JerseyTest 和 Spring 时遇到了困难。以前在非 Spring Java 项目中,我通常为我的 REST APIs 做的是从 JerseyTest 扩展我的测试,模拟服务 classes 和简单的(单元)测试我的 REST API。现在我在我的项目中使用 spring,在我的 REST 资源 classes 中,服务用 @Autowired 注释。现在我正在使用相同的场景。 Spring 插话并唠叨诸如缺少 applicationcontext.xml 之类的事情。我确实想在我的生产中使用 spring 但对于我的单元测试我不需要我的测试知道任何关于 Spring 及其所有自动装配和 class 路径注释处理!我怎样才能做到这一点? classes 看起来像这样:

public class RESTResource{
  @Autowired
  MyService service;

  @GET
  public Response getSomeStuff(){
    ...
    service.getStuff()
  }

}

这是测试 class

public class RESTResourceTest extends JerseyTest{

   private Service service;
   @Override
   public Application configure(){
      RESTResource resource = new RESTResource();
      service = Mockito.mock(Service.class);
      resource.setService(service);
      ResourceConfig config = new ResourceConfig();

      config.register(resource);
      return config;
   }
}

这是堆栈跟踪:

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:344)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:538)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:452)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at org.glassfish.jersey.server.spring.SpringComponentProvider.createXmlSpringConfiguration(SpringComponentProvider.java:173)
    at org.glassfish.jersey.server.spring.SpringComponentProvider.createSpringContext(SpringComponentProvider.java:164)
    at org.glassfish.jersey.server.spring.SpringComponentProvider.initialize(SpringComponentProvider.java:99)
    at org.glassfish.jersey.server.ApplicationHandler.get(ApplicationHandler.java:408)
    at org.glassfish.jersey.server.ApplicationHandler.get(ApplicationHandler.java:399)
    at org.glassfish.jersey.internal.util.collection.Values$LazyValueImpl.get(Values.java:340)
    at org.glassfish.jersey.server.ApplicationHandler.call(ApplicationHandler.java:350)
    at org.glassfish.jersey.server.ApplicationHandler.call(ApplicationHandler.java:347)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:299)
    at org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer.<init>(InMemoryTestContainerFactory.java:77)
    at org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory$InMemoryTestContainer.<init>(InMemoryTestContainerFactory.java:63)
    at org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory.create(InMemoryTestContainerFactory.java:111)
    at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
    at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
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:172)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
    ... 56 more

P.S。我正在使用 spring 启动。

以下是一些可能对您的情况有所帮助的代码片段: 选项 1. 您的 REST 资源 class :

@Component
@Path("/api/helloworld")
public class RESTResource{
@Autowired
MyService service;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String helloMessage() {
    return "Hello World Jersey Way!";
}

}
  1. 你的测试配置 class :

    @Component
    public class JerseyConfig extends ResourceConfig {
    
    /**
     * In constructor we can define Jersey Resources &amp; Other Components
    */
    public JerseyConfig() {
        register(RESTResource.class);
    }
    }
    

3.Your 测试基地 class :

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)//NOTE : Application is a spring boot main class,or if you have specific configuration class similar to test config class above use @ContextConfiguration(classes = MyServiceSpringConfig.class) 
public class RESTResourceTest extends JerseyTest {

@Override
protected Application configure() {
    return new JerseyConfig();
}

@Test
public void contextLoads() {
}

@Test
public void someTest() throws Exception {
    // Very useful test
}


}

选项 2:

@Priority(value = 1)
public class MySpringWebInitializer implements WebApplicationInitializer
{
    @Override
    public void onStartup(ServletContext container)
    {
        //Tell jersey-spring3 the context is already initialized
        container.setInitParameter("contextConfigLocation", "NOTNULL");
        AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
        appContext.register(RESTResource.class);
        container.addListener(new ContextLoaderListener(appContext));
    }
}

此外,对于 spring 引导,您可以 运行 使用活动配置文件或配置名称测试用例,通过 -Dspring.config.name=test 传递 运行time VM args 运行宁测试用例等..

希望这对您有所帮助,其余的不言自明。

您可以做的是在测试阶段使用 sure-fire 插件简单地排除 jersey-spring3。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <classpathDependencyExcludes>
                        <classpathDependencyExclude>
                            org.glassfish.jersey.ext:jersey-spring3
                        </classpathDependencyExclude>
                    </classpathDependencyExcludes>
                </configuration>
            </plugin> 
The better way to integrate JerseyTest with Spring
  • junit 测试中没有继承 JerseyTest class
  • 在 junit 测试中没有对 Jersey 的依赖class,只是纯粹的 jax-rs 依赖
  • 完美整合SpringJUnit4ClassRunner和@ContextConfiguration

我的解决方案托管在 github 上。希望对你有所帮助。