Bad type on the operand stack in arraylength

2019-03-24 05:29发布

问题:

I am trying to test a method using powermock. i haven't written any test cases yet. Just trying to set up the class for Mocking. here is what I have so far:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ReadRubric.class })
public class ReadRubricTest {
    @Before
    public void setUp() throws Exception {
        PowerMockito.mock(ReadRubric.class);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void invalidFile() throws Exception {
    }
}

When I try to run this test I get the following error.

java.lang.VerifyError: Bad type on operand stack in arraylength
    Exception Details:
      Location:
        com/cerner/devcenter/wag/processor/ReadRubric.readRubric()Ljava/util/List; @809: arraylength
      Reason:
        Invalid type: 'java/lang/Object' (current frame, stack[0])
      Current Frame:
        bci: @809
        flags: { }
        locals: { 'java/util/ArrayList', 'au/com/bytecode/opencsv/CSVReader', top, top, '[Ljava/lang/String;', 'com/cerner/devcenter/wag/processor/Rule', 'java/lang/Object', 'java/lang/Object', top, top, top, top, top, top, top, 'java/lang/Object' }
        stack: { 'java/lang/Object' }

java -version on cmd gives me 1.7.0_65

java_home is set to C:\Program Files\Java\jdk1.7.0_65

On eclipse I am using jre 7 and the compiler level is 1.7

In my maven, I have added the following dependency to allow switch cases that accept Strings instead of chars:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
         </plugin>

can anyone help me in solving this issue?

Here is the method that I am testing:

public static List<Rule> readRubric() throws WAGException, IOException {
        List<Rule> ruleList = new ArrayList<Rule>();
        CSVReader rubricReader = null;
            File file = new File(RUBRIC_FILE_NAME);
            FileReader fileReader = new FileReader(file);
            rubricReader = new CSVReader(fileReader);
            String[] ruleRecord = null;
            //rubricReader.readNext();
            while ((ruleRecord = rubricReader.readNext()) != null) {
                Rule rule = new Rule();
                rule.setRuleName(ruleRecord[0].trim());
                String parameters[] = ruleRecord[1].split(",");
                rule.addParameter(Arrays.asList(parameters));
                rule.setPoints(Integer.parseInt(ruleRecord[2].trim()));
                rule.setRuleEngine(ruleRecord[3].trim());
            rule.setComment(ruleRecord[4].trim());
                    ruleList.add(rule);
        }
            rubricReader.close();
        return ruleList;
    }

I found the solution online: it says that i need to add -XX:-UseSplitVerifier to my VM arguments when I run the test. The test runs when I do this. But I need it to run with maven, for that the solution says that I need to add the following plugin to maven:

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

But even after adding this to my POM, this particulair test fails. Link to the solution

回答1:

So after working on it for hours, I found the root cause of the problem. In Java 7u65, they made the byte code verifier more strict as a result, the byte code verifier is reading PowerMock byte code and throwing a VerifyError. So ,basically, PowerMock's byte code is not passing the byte code verifier. The only way around is add -XX:-UseSplitVerifier as VM arguments or to switch to Java 6.

In my case, when I remove ruleRecord[1].split(",") and when I implement my own split functionality, the tests work just fine. I guess Powermock messes up the bytecode when we use,for e.g, methods like whenNew(). Hope this helps.