Bad <init> method call from inside of a branch

Lauri picture Lauri · Aug 21, 2014 · Viewed 13.5k times · Source

After upgrading JDK to java7u65 several unit-tests utilizing Mockito and PowerMock started to fail with following causes:

15:15:23,807 INFO  - Tests in error: 
15:15:23,810 INFO  -   initializationError(com.your.ClassHere): Bad <init> method call from inside of a branch

Answer

Lauri picture Lauri · Aug 21, 2014

UPDATE

There are newer java7 versions that fix this issue. As is written in reply to Powermock / Javassist creates illegal constructors for JDK 1.7.0u65 and 1.8.0u11 with -target 7 #525

upgrading from java 7u71 to 7u75 fixed the problem

Rootcause

Upon investigation I heard cries for help from all over the internet from all tools and languages that built upon JDK.

Turns out the cause is new java bytecode standard that gets checked by new verifier. But unfortunately javassist sometimes is asked by powermock to produce changes into bytecode that are no longer accepted by this new shiny veryfier.

Workaround (for those who cannot go with newer java)

As a workaround in JRebel blog they suggested using -noverify flag upon starting JVM However i found from Java 7 Bytecode Verifier: Huge backward step for the JVM blog post alternative workaround that works on java7: -XX:-UseSplitVerifier

As my tests run in some unaccessible server and are executed as part of maven build, i needed to find a way to pass that argument along with my project files. First workable solution i discovered is to add this parameter to configurtaion of surefire plugin in pom.xml as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
            <configuration>
                <argLine>-XX:-UseSplitVerifier</argLine>
            </configuration>
        </plugin>
        <plugin>
    </plugins>
</build>

I suppose that on java8 one could use similar method to call tests with -noverify key, but haven't had the chance to confirm that.

Other related resources to keep eye on

Powermock / Javassist creates illegal constructors for JDK 1.7.0u65 and 1.8.0u11 with -target 7. Powermock / Javassist creates illegal constructors for JDK 1.7.0u65 and 1.8.0u11 with -target 7 #525