4

I made a test case to test my Rest Web Service. but in test case I am seeing that the request is going to default port of jersey test framework which is http://localhost:9998 whereas my service is register on http://localhost:8080. I am unable to find that how can I change its port to 8080

public class UMServiceTest extends JerseyTest {


    @Override
    public Application configure() {
        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);
        return new ResourceConfig(UMService.class);
    }    


    @Test
    public void testFetchAll() {
        System.out.println(getBaseUri()+"==========");
        Response output = target("usermanagement").path("um").path("user").request().get();
        assertEquals("should return status 200", 200, output.getStatus());
        //assertNotNull("Should return list", output.getEntity());
    }
Despicable
  • 3,797
  • 3
  • 24
  • 42

4 Answers4

5

you can give a command line arguments when you run the test such as,

Maven mvn yourpack.UMServiceTest -Djersey.config.test.container.port=8080

or in eclipse you can pass this in run config 'Arguments' tab

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
5

You could change the Systemproperty from TestProperties for the JerseyTest too.

public class UMServiceTest extends JerseyTest
{
    static
    {
        System.setProperty("jersey.config.test.container.port", "0");
    }
...
GF. Service
  • 108
  • 1
  • 8
2

In addition to kuhajeyan's answer here's maven config for JerseyTest port:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                ....
                <systemProperties>
                    <property>
                        <name>jersey.config.test.container.port</name>
                        <value>4410</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
Cyrusmith
  • 747
  • 1
  • 9
  • 17
1

As of Jersey 2.33, the configure method can be set as in the following example taken from the Jersey Docs

@Override
protected Application configure() {
    // Find first available port.
    forceSet(TestProperties.CONTAINER_PORT, "0");

    return new ResourceConfig(Resource.class);
}

In case, the configureDeployment is used to specify the resource, the below approach can be used.

  @BeforeClass
  public static void beforeClass() {
    System.setProperty(
        // Use a random available port
        "jersey.config.test.container.port", String.valueOf(SocketUtils.findAvailableTcpPort()));
  }

  @AfterClass
  public static void afterClass() {
    System.clearProperty("jersey.config.test.container.port");
  }

  @Override
  protected DeploymentContext configureDeployment() {
    return ServletDeploymentContext.forServlet(
            new ServletContainer(ResourceConfig.forApplicationClass(TestApplication.class)))
        .contextPath("/cas/api")
        .build();
  }

Source: https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/test-framework.html#parallel

Debargha Roy
  • 2,320
  • 1
  • 15
  • 34