使用PowerMock导致java.lang.VerifyError异常

时间:2016-07-20 07:18:34

标签: java maven junit mockito powermock

以下内容在运行时导致异常:

import com.sun.net.httpserver.HttpServer;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import java.net.InetSocketAddress;

@RunWith(PowerMockRunner.class)
public class MainTest {
    @org.junit.Test
    public void main() throws Exception {
        HttpServer.create(new InetSocketAddress(8080), 0);
    }
}

例外:

  

java.lang.VerifyError:操作数堆栈上的错误类型   例外细节:    地点:      COM /阳光/网/的httpserver / SPI / HttpServerProvider $ 1.run()Ljava /郎/对象; @ 27:invokestatic    原因:      键入' sun / net / httpserver / DefaultHttpServerProvider' (当前帧,堆栈[0])不能分配给' com / sun / net / httpserver / spi / HttpServerProvider'    当前框架:      bci:@ 27      旗帜:{}      当地人:{' com / sun / net / httpserver / spi / HttpServerProvider $ 1' }      stack:{' sun / net / httpserver / DefaultHttpServerProvider' }    字节码:      0x0000000:b800 2599 0007 b800 27b0 b800 2699 0007      0x0000010:b800 27b0 bb00 1b59 b700 2ab8 0028 57b8      0x0000020:0027 b0
   Stackmap表:      same_frame(@ 10)      same_frame(@ 20)

     

at com.sun.net.httpserver.spi.HttpServerProvider.provider(httpServerProvider.java:165)     在com.sun.net.httpserver.HttpServer.create(HttpServer.java:129)     在HelloWorldByHour.main(HelloWorldByHour.java:16)     在HelloWorldByHourTest.testMain(HelloWorldByHourTest.java:37)     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)     在org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)     at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)     在org.junit.internal.runners.MethodRoadie $ 2.run(MethodRoadie.java:86)     在org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)     at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)     at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)     在org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)     在org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)     在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)     在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)     在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ 1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)     在org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)     在org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)     在org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)     在org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)     在org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)     在org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)     在org.junit.runner.JUnitCore.run(JUnitCore.java:159)     在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)     在com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)     在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>testPowerMock</groupId>
    <artifactId>testPowerMock</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>(whatever version is current)</version>
                <configuration>
                    <!-- or whatever version you use -->
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-core</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.6.5</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

我认为异常与PowerMock版本有关,因为删除行@RunWith(PowerMockRunner.class)会使&#34;测试&#34;毫无例外地运行。

知道那里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

如果您使用的是Java 7,则可以使用&#34; -XX避免使用StackMap帧:-UseSplitVerifier&#34;作为JVM的参数。

您可以找到更多信息here