【发布时间】:2015-11-10 08:35:19
【问题描述】:
我在 Jersey 测试中遇到了 HttpServer 的问题。如果我将所有测试包含在一个中,它可以正常工作,但是如果我有两个或更多,那么在通过其中一个之后,它会抛出 exceprion javax.ws.rs.ProcessingException: IOException 在创建 JDK HttpServer 时引发 原因: java.net.BindException:地址已在使用中:绑定。 调试显示它尝试再次启动服务器。如何修复这个错误? 提前致谢。
这是我的配置
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());
}
堆栈跟踪
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)
【问题讨论】:
-
你能发布包含多个测试的代码吗?所以我们可以检查一下。
-
我发布了测试,但没有 DB 就没有帮助。
-
在 org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277) 在 org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609) 这些方法(setUp () 和 createTestContainer()) 丢失。那就是你得到错误的地方。你能用整个不工作的测试类来更新答案吗?
-
Aurasphere,这是全班。也许你需要其他人?
-
对不起,我看错了。检查我的答案。
标签: java web-services http testing jersey