How to specify context path for jersey test with external provider

后端 未结 2 1978
梦毁少年i
梦毁少年i 2021-01-26 01:52

I want my jersey tests to run on one instance of tomcat which has the rest services running at

 http://myhost:port/contexpath/service1/ 
 http://myhost:port/con         


        
相关标签:
2条回答
  • If you have your external servlet:

    Import the jersey-test-framework-core apis to implement your own TestContainerFactory

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

    .

    Let JerseyTest know you will have your own provider through SystemProperties

    systemProperty 'jersey.config.test.container.factory', 'my.package.MyTestContainerFactory'
    

    .

    Create your own provider (better and more custom configurable than their 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
                }
            };
        }
    }
    
    0 讨论(0)
  • 2021-01-26 02:43

    Finally I figured out a solution

     @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);
          }
          };
      }
    
    0 讨论(0)
提交回复
热议问题