我正在尝试从被测系统所需的服务中创建模拟。我正在使用MockServer(mock-server.com)。这是模拟服务的代码。
ClientAndServer mockServer = startClientAndServer(1080);
QueryResultResponse response = new QueryResultResponse();
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String jsonStr = ow.writeValueAsString(response);
new MockServerClient("127.0.0.1", 1080)
.when(
request()
.withMethod("POST"),
Times.unlimited()
)
.respond(
response()
.withStatusCode(200)
.withBody(jsonStr)
.withHeader(new Header("Content-Type", "application/json"))
);
当我尝试从JUnit规则执行这段代码时,线程在java.net.SocketInputStream.socketRead0上被阻塞,因此无法执行将使用此mock的单元测试。这是块调用的堆栈。
(SocketInputStream.java) java.net.SocketInputStream.socketRead0
(SocketInputStream.java:152) java.net.SocketInputStream.read
(SocketInputStream.java:122) java.net.SocketInputStream.read
(LoggingInputStream.java:72) org.apache.http.impl.conn.LoggingInputStream.read
(SessionInputBufferImpl.java:136) org.apache.http.impl.io.SessionInputBufferImpl.streamRead
(SessionInputBufferImpl.java:152) org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer
(SessionInputBufferImpl.java:270) org.apache.http.impl.io.SessionInputBufferImpl.readLine
(DefaultHttpResponseParser.java:140) org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead
(DefaultHttpResponseParser.java:57) org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead
(AbstractMessageParser.java:260) org.apache.http.impl.io.AbstractMessageParser.parse
(DefaultBHttpClientConnection.java:161) org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader
(NativeMethodAccessorImpl.java) sun.reflect.NativeMethodAccessorImpl.invoke0
(NativeMethodAccessorImpl.java:57) sun.reflect.NativeMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43) sun.reflect.DelegatingMethodAccessorImpl.invoke
(Method.java:606) java.lang.reflect.Method.invoke
(CPoolProxy.java:138) org.apache.http.impl.conn.CPoolProxy.invoke
() com.sun.proxy.$Proxy73.receiveResponseHeader
(HttpRequestExecutor.java:271) org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse
(HttpRequestExecutor.java:123) org.apache.http.protocol.HttpRequestExecutor.execute
(MainClientExec.java:253) org.apache.http.impl.execchain.MainClientExec.execute
(ProtocolExec.java:194) org.apache.http.impl.execchain.ProtocolExec.execute
(RetryExec.java:85) org.apache.http.impl.execchain.RetryExec.execute
(RedirectExec.java:108) org.apache.http.impl.execchain.RedirectExec.execute
(InternalHttpClient.java:186) org.apache.http.impl.client.InternalHttpClient.doExecute
(CloseableHttpClient.java:82) org.apache.http.impl.client.CloseableHttpClient.execute
(CloseableHttpClient.java:106) org.apache.http.impl.client.CloseableHttpClient.execute
(ApacheHttpClient.java:103) org.mockserver.client.http.ApacheHttpClient.sendPUTRequest
(MockServerClient.java:155) org.mockserver.client.server.MockServerClient.sendExpectation
(ForwardChainExpectation.java:23) org.mockserver.client.server.ForwardChainExpectation.respond
(TestMockService.java:74) oracle.sysman.test.unit.TestMockService.call
(TestLoganUploadServiceUnit.java:99) oracle.sysman.test.unit.ws.TestLoganUploadServiceUnit.startMockServer
(NativeMethodAccessorImpl.java) sun.reflect.NativeMethodAccessorImpl.invoke0
(NativeMethodAccessorImpl.java:57) sun.reflect.NativeMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43) sun.reflect.DelegatingMethodAccessorImpl.invoke
(Method.java:606) java.lang.reflect.Method.invoke
(FrameworkMethod.java:47) org.junit.runners.model.FrameworkMethod$1.runReflectiveCall
(ReflectiveCallable.java:12) org.junit.internal.runners.model.ReflectiveCallable.run
(FrameworkMethod.java:44) org.junit.runners.model.FrameworkMethod.invokeExplosively
(RunBefores.java:24) org.junit.internal.runners.statements.RunBefores.evaluate
(MockServerRule.java:107) org.mockserver.junit.MockServerRule$1.evaluate
(RunRules.java:20) org.junit.rules.RunRules.evaluate
(ParentRunner.java:271) org.junit.runners.ParentRunner.runLeaf
(BlockJUnit4ClassRunner.java:70) org.junit.runners.BlockJUnit4ClassRunner.runChild
(BlockJUnit4ClassRunner.java:50) org.junit.runners.BlockJUnit4ClassRunner.runChild
(ParentRunner.java:238) org.junit.runners.ParentRunner$3.run
(ParentRunner.java:63) org.junit.runners.ParentRunner$1.schedule
(ParentRunner.java:236) org.junit.runners.ParentRunner.runChildren
(ParentRunner.java:53) org.junit.runners.ParentRunner.access$000
(ParentRunner.java:229) org.junit.runners.ParentRunner$2.evaluate
(ParentRunner.java:309) org.junit.runners.ParentRunner.run
(Suite.java:127) org.junit.runners.Suite.runChild
(Suite.java:26) org.junit.runners.Suite.runChild
(ParentRunner.java:238) org.junit.runners.ParentRunner$3.run
(ParentRunner.java:63) org.junit.runners.ParentRunner$1.schedule
(ParentRunner.java:236) org.junit.runners.ParentRunner.runChildren
(ParentRunner.java:53) org.junit.runners.ParentRunner.access$000
(ParentRunner.java:229) org.junit.runners.ParentRunner$2.evaluate
(RunBefores.java:26) org.junit.internal.runners.statements.RunBefores.evaluate
(RunAfters.java:27) org.junit.internal.runners.statements.RunAfters.evaluate
(ParentRunner.java:309) org.junit.runners.ParentRunner.run
(JUnit4Testable.java:24) oracle.jdevimpl.junit.runner.junit4.JUnit4Testable.run
(TestExecution.java:27) oracle.jdevimpl.junit.runner.TestExecution.run
(JUnitTestRunner.java:93) oracle.jdevimpl.junit.runner.JUnitTestRunner.main