'Using PowerMock causes java.lang.VerifyError exception

The following causes an exception at runtime:

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);
    }
}

The exception:

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 HelloWorldByHour.main(HelloWorldByHour.java:16) at 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) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310) at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86) at 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) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106) at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) at org.junit.runner.JUnitCore.run(JUnitCore.java:159) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at 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>

I think the exception has something to do with the PowerMock versions since removing the line @RunWith(PowerMockRunner.class) makes the "test" run without exception.

Any idea what's wrong there?



Solution 1:[1]

If you are using Java 7 you can avoid StackMap frames using "-XX:-UseSplitVerifier" as a parameter to the JVM.

You can find more info here

Solution 2:[2]

Add @PowerMockIgnore({"sun.net.*", "com.sun.*"}) in your unit test file may help.

Solution 3:[3]

As a possibility, please verify that the combination of Powermock, Mockito, Junit, Hamcrest library versions is a correct one (depends on what you are using).

I've been having a similar issue with an error complaining about Mockito not being able to mock a class a private or final class (exception details showed a VerifyError when assigning an Object to a Throwable) and the problem was a mismatch of versions between test / mock libraries.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rodrigo Villalba Zayas
Solution 2 mingzhen hou
Solution 3 Tudor Vintilescu