java - 运行多个方法时 Jersey 测试地址已被使用

标签 java jersey

我正在尝试使用继承自 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/

相关文章:

java - 错误 : ArrayOutOfBounds. 如何解决?

java - Java 程序如何检测到 MacOS 机器正在关闭?

json - 在 Jersey 2.2 JAX-RS 下使用 JAXB 和 JSON 的命名空间

dependency-injection - 泽西 @Context 范围

java - 如何使用 jersey 和 json 在 java 中检索 Restful Web 服务的有效负载数据

java - POST 到 Jersey REST 服务得到错误 415 Unsupported Media Type

rest - "Access-Control-Allow-Origin:*"对 REST Web 服务没有影响

java - Spring Boot 中基于方法的授权

java - 如何将值从公共(public)类传递到另一个公共(public)类?

java - 了解有效的 Java 深拷贝示例