jerseyjersey-2.0

How to specify context path for jersey test with external provider


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/contexpath/service2/ 
 ..so on

I have both in memory and external container dependencies

[ 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'],

Then in the test i over ride the below method to decide which container to choose

  @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();
  }    

The in memory test works because the services are deployed by the framework at path which it knows(it does not have a context path anyway) When i run on external container it tries to connect to http://myhost:port/service1/ instead of http://myhost:port/contexpath/service1/ thus getting 404 not found

To run on an external container how do i specify the context path ? The documentation specifies only host and port property.Is there any property for context path ?

I am using Jersey 2.17


Solution

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