如何使用外部提供程序为泽西岛测试指定上下文路径

时间:2015-07-12 10:16:54

标签: jersey jersey-2.0

我希望我的泽西测试在tomcat的一个实例上运行,其余的服务在

运行
 http://myhost:port/contexpath/service1/ 
 http://myhost:port/contexpath/service2/ 
 ..so on

我同时拥有内存和外部容器依赖

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

然后在测试中我骑过下面的方法来决定选择哪个容器

  @Override
  public TestContainerFactory getTestContainerFactory() {
    System.setProperty("jersey.test.host", "localhost");
    System.setProperty("jersey.config.test.container.port", "8000");
    //how to set the context path ??
    return new ExternalTestContainerFactory();
  }    

内存测试有效,因为服务是由框架在它知道的路径上部署的(它无论如何都没有上下文路径) 当我在外部容器上运行时,它会尝试连接到http://myhost:port/service1/而不是http://myhost:port/contexpath/service1/,因此找不到404

要在外部容器上运行,如何指定上下文路径? 该文档仅指定主机和端口属性。是否存在上下文路径的任何属性?

我正在使用Jersey 2.17

2 个答案:

答案 0 :(得分:2)

最后我想出了一个解决方案

 @Override
  public TestContainerFactory getTestContainerFactory() {
    return new ExternalTestContainerFactory(){

      @Override
      public TestContainer create(URI baseUri, DeploymentContext context)
          throws IllegalArgumentException {
        try {
          baseUri = new URI("http://localhost:8000/contextpath");
        } catch (URISyntaxException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        return super.create(baseUri, context);
      }
      };
  }

答案 1 :(得分:1)

如果你有外部servlet:

导入jersey-test-framework-core apis以实现自己的TestContainerFactory

testCompile 'org.glassfish.jersey.test-framework:jersey-test-framework-core:2.22.2'

让JerseyTest知道您将通过SystemProperties

拥有自己的提供商
systemProperty 'jersey.config.test.container.factory', 'my.package.MyTestContainerFactory'

创建您自己的提供商(比jersey-test-framework-provider-external

更好,更可自定义)
import org.glassfish.jersey.test.spi.TestContainer;
import org.glassfish.jersey.test.spi.TestContainerFactory;


public class MyTestContainerFactory implements TestContainerFactory {


    @Override
    public TestContainer create(URI baseUri, DeploymentContext deploymentContext) {
        return new TestContainer(){

            @Override
            public ClientConfig getClientConfig() {
                return null;
            }

            @Override
            public URI getBaseUri() {
                return URI.create("http://localhost:8080/myapp/api");
            }

            @Override
            public void start() {
                // Do nothing
            }

            @Override
            public void stop() {
                // Do nothing
            }
        };
    }
}
相关问题