运行多个方法时,Jersey测试地址已在使用中

PKu*_*uhn 11 java jersey

我试图用继承自JerseyTest的类来测试我的rest接口.虽然第一个测试方法成功没有任何问题,但以下测试失败,绑定异常地址已在使用中.我是否需要在测试之间释放某种资源才能使其运行?

class MyClass extends JerseyTest () {
    @Override
    protected Application configure() {
        return new ResourceConfig(MyClass.class);
    }
    @Test
    testFirstThing() {
        // test some request  
    }
    @Test
    testFirstThing() {
        // test another request  
    }
}
Run Code Online (Sandbox Code Playgroud)

kma*_*794 15

我今天遇到了同样的问题,一些测试通过了,但有些测试失败了

Jersey Test文档中有一个解决方案:https: //jersey.java.net/documentation/latest/test-framework.html#parallel

为了并行运行多个测试容器,您需要将TestProperties.CONTAINER_PORT设置为0.这将告诉Jersey Test Framework(和底层测试容器)使用第一个可用端口.

@Override
protected Application configure() {
    forceSet(TestProperties.CONTAINER_PORT, "0");
    return new ResourceConfig(MyClass.class);
}
Run Code Online (Sandbox Code Playgroud)