Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey test address already in use when running multiple methods

Tags:

java

jersey

I am trying to test my rest interface with a class that inherits from JerseyTest. While the first test method succeeds without any problem, the following tests fail with an bind exception Address already in use. Do I need to release some kind of resource in between the tests to get this running?

class MyClass extends JerseyTest () {
    @Override
    protected Application configure() {
        return new ResourceConfig(MyClass.class);
    }
    @Test
    testFirstThing() {
        // test some request  
    }
    @Test
    testFirstThing() {
        // test another request  
    }
}
like image 649
PKuhn Avatar asked Apr 26 '16 13:04

PKuhn


1 Answers

I had the same issue today, some of the tests passed, but some failed with BindException

There is a solution in Jersey Test documentation: https://jersey.java.net/documentation/latest/test-framework.html#parallel

For a purpose of running multiple test containers in parallel you need to set the TestProperties.CONTAINER_PORT to 0 value. This will tell Jersey Test Framework (and the underlying test container) to use the first available port.

@Override
protected Application configure() {
    forceSet(TestProperties.CONTAINER_PORT, "0");
    return new ResourceConfig(MyClass.class);
}
like image 177
kma8794 Avatar answered Nov 05 '22 13:11

kma8794