我试图通过尝试模拟HttpServer来进行一些单元测试。当我运行此代码时,它给了我错误: -
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.net.ssl.*", "javax.management.*"})
public class POSTServerTest {
private String endPoint = "http://localhost:30310/api/endpoint/";
private String connectionTimeOut = "3000";
private String readTimeOut = "3000";
private HttpServer server;
@Captor
ArgumentCaptor<Logger> loggerArgumentCaptor;
@Captor
ArgumentCaptor<String> stringArgumentCaptor;
@Before
public void setUp() throws IOException {
MockitoAnnotations.initMocks(this);
server = HttpServer.create(new InetSocketAddress(8080), 0);
server.start();
}
@Test
public void testReadTimeOutException() throws Exception {
setConnectionTimeOut("3000");
setReadTimeOut("1");
//some code here
}
}
我收到此错误: -
"C:\Program Files\Java\jdk1.8.0_131\bin\java"
java.lang.VerifyError: Bad type on operand stack
Exception Details:
Location:
com/sun/net/httpserver/spi/HttpServerProvider$1.run()Ljava/lang/Object; @27:
invokestatic
Reason:
Type 'sun/net/httpserver/DefaultHttpServerProvider' (current frame,
stack[0]) is not assignable to
'com/sun/net/httpserver/spi/HttpServerProvider'
Current Frame:
bci: @27
flags: { }
locals: { 'com/sun/net/httpserver/spi/HttpServerProvider$1' }
stack: { 'sun/net/httpserver/DefaultHttpServerProvider' }
Bytecode:
0x0000000: b800 2599 0007 b800 27b0 b800 2699 0007
0x0000010: b800 27b0 bb00 1b59 b700 2ab8 0028 57b8
0x0000020: 0027 b0
Stackmap Table:
same_frame(@10)
same_frame(@20)
at com.sun.net.httpserver.spi.HttpServerProvider.provider(HttpServerProvider.java:165)
at com.sun.net.httpserver.HttpServer.create(HttpServer.java:129)
at no.bbs.tt.oalarmer.vos.POSTServerTest.setUp(POSTServerTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:132)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:95)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我正在尝试使用maven构建它,这是它正在使用的java版本的maven输出: -
Picked up JAVA_TOOL_OPTIONS: -Djava.net.preferIPv4Stack=true
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-
03T21:39:06+02:00)
Maven home: C:\Users\tshak\Downloads\apache-maven-3.5.0-bin\apache-maven-
3.5.0\bin\..
Java version: 1.8.0_131, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_131\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
任何人都可以帮我运行HttpServer的示例代码吗? 任何帮助你真的很感激。 :)
答案 0 :(得分:2)
@PowerMockIgnore({"sun.net.*", "com.sun.*"})
public class POSTServerTest {...
问题在于PowerMock。添加要忽略的其他类将通过阻止这些类的字节代码验证来实现。
答案 1 :(得分:0)
@PowerMockIgnore(“ sun.net.httpserver。”,“ com.sun.net.httpserver ”) 公共类POSTServerTest {...