我正在尝试使用继承自 JerseyTest 的类来测试我的其余接口(interface)。虽然第一个测试方法没有任何问题成功,但以下测试失败并出现绑定(bind)异常地址已在使用中。我是否需要在测试之间释放某种资源才能使其运行?
class MyClass extends JerseyTest () {
@Override
protected Application configure() {
return new ResourceConfig(MyClass.class);
}
@Test
testFirstThing() {
// test some request
}
@Test
testFirstThing() {
// test another request
}
}
最佳答案
我今天遇到了同样的问题,一些测试通过了,但一些测试失败并出现 BindException
Jersey Test文档中有一个解决方案: 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);
}
关于java - 运行多个方法时 Jersey 测试地址已被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36866693/