Java Jersey测试启动HTTP服务器两次

Java Jersey测试启动HTTP服务器两次,java,web-services,http,testing,jersey,Java,Web Services,Http,Testing,Jersey,我在泽西岛测试中遇到HttpServer问题。如果我将所有测试都包含在一个测试中,那么它可以正常工作,但是如果我有两个或更多测试,那么在通过其中一个测试后,它会在创建JDK HttpServer时抛出exceprion javax.ws.rs.ProcessingException:IOException,原因是:java.net.BindException:地址已在使用中:bind。 调试显示它试图再次启动服务器。如何修复此错误? 提前谢谢 这是我的配置 public class SomeJe

我在泽西岛测试中遇到HttpServer问题。如果我将所有测试都包含在一个测试中,那么它可以正常工作,但是如果我有两个或更多测试,那么在通过其中一个测试后,它会在创建JDK HttpServer时抛出exceprion javax.ws.rs.ProcessingException:IOException,原因是:java.net.BindException:地址已在使用中:bind。 调试显示它试图再次启动服务器。如何修复此错误? 提前谢谢

这是我的配置

public class SomeJerseyTest extends JerseyTest {

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
    return new JdkHttpServerTestContainerFactory();

}


@Override
protected Application configure() {

    System.setProperty(IDBPoolConstants.PROP_POOL_URL, "jdbc:mysql://localhost/ets_test");
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getAttribute("ets")).thenReturn(TestUtils.loginAsSU());
    return new ResourceConfig().register(UpdateReportResource.class).register(ReportResource.class).register(new AbstractBinder() {
        @Override
        protected void configure() {
            bind(request).to(HttpServletRequest.class);
        }
    });
}

 @Test
 public void testPutTexStatusOk() {
    TexStatusDTO dto = new TexStatusDTO();
    dto.setAuthor("Padme Amidala");
    dto.setCode(3);
    dto.setDescription("testPutTexStatusOk");
    dto.setReportId(11);
    dto.setCreated(new Date());
    Response response = target().path("reports/texStatus").request().accept(MediaType.APPLICATION_JSON)
            .buildPut(Entity.entity(dto, MediaType.APPLICATION_JSON)).invoke();
    System.out.println(response.toString());

}

 @Test
 public void testCode200 () {
        final Response response = target("/reports/reports").queryParam("projectId", "63").request().get();
        System.out.println(response);
        assertEquals(200, response.getStatus());
 }
Stacktrace

javax.ws.rs.ProcessingException: IOException thrown when creating the JDK HttpServer.
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:262)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:206)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:111)
at org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory$JdkHttpServerTestContainer.<init>(JdkHttpServerTestContainerFactory.java:82)
at org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory$JdkHttpServerTestContainer.<init>(JdkHttpServerTestContainerFactory.java:67)
at org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory.create(JdkHttpServerTestContainerFactory.java:125)
at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Caused by: java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:444)
at sun.nio.ch.Net.bind(Net.java:436)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at sun.net.httpserver.ServerImpl.<init>(ServerImpl.java:100)
at sun.net.httpserver.HttpServerImpl.<init>(HttpServerImpl.java:50)
at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(DefaultHttpServerProvider.java:35)
at com.sun.net.httpserver.HttpServer.create(HttpServer.java:129)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:259)
javax.ws.rs.ProcessingException:创建JDK HttpServer时引发IOException。
位于org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:262)
位于org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:206)
位于org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:111)
位于org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory$JdkHttpServerTestContainerFactory。(JdkHttpServerTestContainerFactory.java:82)
位于org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory$JdkHttpServerTestContainerFactory(JdkHttpServerTestContainerFactory.java:67)
位于org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory.create(JdkHttpServerTestContainerFactory.java:125)

位于org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
位于org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中 位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50) 位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47) 位于org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 位于org.junit.internal.runners.statements.runafter.evaluate(runafter.java:27) 位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 位于org.junit.runners.ParentRunner.run(ParentRunner.java:363) 位于org.junit.runner.JUnitCore.run(JUnitCore.java:137) 位于com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) 位于com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) 位于com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)中 位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 原因:java.net.BindException:地址已在使用中:bind 位于sun.nio.ch.Net.bind0(本机方法) 位于sun.nio.ch.Net.bind(Net.java:444) 位于sun.nio.ch.Net.bind(Net.java:436) 位于sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214) 位于sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) 位于sun.net.httpserver.ServerImpl.(ServerImpl.java:100) 位于sun.net.httpserver.HttpServerImpl.(HttpServerImpl.java:50) 位于sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(DefaultHttpServerProvider.java:35) 位于com.sun.net.httpserver.httpserver.create(httpserver.java:129) 位于org.glassfish.jersey.jdkhttp.JdkHttpServerFactory.createHttpServer(JdkHttpServerFactory.java:259)
这里发生的事情是,正如您所说,容器被实例化了两次。您可能可以使用singleton模式轻松解决此问题:

private JdkHttpServerTestContainerFactory containerFactory;

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
if(containerFactory == null) containerFactory = new JdkHttpServerTestContainerFactory(); 
return containerFactory;
}

只需添加此字段并更改getTestContainerFactory方法,它就可以工作。

您可以发布包含多个测试的代码吗?所以我们可以检查它。我发布了测试,但如果没有DB它就没有帮助。org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)缺少这些方法(setUp()和createTestContainer()。这就是你得到错误的地方。你能用不起作用的整个测试类更新答案吗?Aurasphere,这是整个类。也许你需要其他人?对不起,我看错了。请检查我的答案。不幸的是,这没有帮助,但感谢您的尝试。好吧,这是一个黑魔法,因为我创建了一个新文件,复制了那里的所有内容,效果很好。很高兴听到这个消息!