如何使用外部提供程序在Jersey测试中设置spring上下文

时间:2015-08-21 11:05:26

标签: java spring jersey

我想与外部提供商一起运行泽西岛测试

[group: 'org.glassfish.jersey.test-framework.providers', name: 'jersey-test-framework-provider-external' , version: '2.17']

现在我有一个正在运行的服务器,在启动时使用spring应用程序上下文连接到数据库(比如主数据库) 现在我想使用外部提供程序运行我的JerseyTest来在此服务器上运行我的测试,但是使用另一个spring上下文来使用测试数据库。这可能吗? 这是我试图的

  public class MyTest extends JerseyTest {
//Already running server where application is deployed
      private static final String MY_TEST_URL = "http://localhost:8080/somepath";

      private static class MyTestContainer extends ExternalTestContainerFactory {
        private  DeploymentContext customContext;
        public MyTestContainer(DeploymentContext configureDeployment) {
          this.customContext=configureDeployment;
        }

        @Override
        public TestContainer create(URI baseUri, DeploymentContext context)
            throws IllegalArgumentException {
          try {
            baseUri = new URI(MY_TEST_URL);
          } catch (URISyntaxException e) {
            throw new RuntimeException(e);
          }
          return super.create(baseUri, customContext);
        }
      }

        @Override
      protected DeploymentContext configureDeployment() {
         ResourceConfig config = new ResourceConfig(MyRestResource.class);
         config.packages("com.myapp.restful");
         ApplicationContext context = new ClassPathXmlApplicationContext("MyTest-applicationContext.xml");
         config.property("contextConfig", context);
         DeploymentContext custom=DeploymentContext.builder(config).build();
         return custom; //return a deployment context with different spring context
         };
      private  final ExternalTestContainerFactory testContainer = new MyTestContainer(this.configureDeployment());

       @Override
      public TestContainerFactory getTestContainerFactory() {
       return testContainer;
     }


      @Test
      public void testCreateResouce(){

        target(path).request().post(entity); //post some entity to the server
      }

    }

但是由于已经运行的服务器使用了一个使用“主数据库”的弹簧配置,因此在“主数据库”中创建了实体。所有在测试中完成的配置都被忽略了? 如果服务器上尚未部署任何内容,则会提供404响应。 测试的配置如何工作?

0 个答案:

没有答案