arraylength中操作数堆栈上的错误类型 [英] Bad type on the operand stack in arraylength
问题描述
我正在尝试使用powermock测试方法。我还没有写过任何测试用例。只是想为Mocking设置课程。这是我到目前为止:
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给了我1.7.0_65
java_home设置为C:\Program Files \ Java \ _jdk1.7.0_65
在eclipse上我使用的是jre 7,编译器级别为1.7
在我的maven中,我添加了以下依赖项以允许接受字符串而不是字符的开关案例:
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;
}
我在网上找到了解决方案:它说我需要添加<$ c我运行测试时,$ c> -XX:-UseSplitVerifier 到我的VM参数。当我这样做时,测试运行。但我需要它与maven一起运行,因为解决方案说我需要将以下插件添加到maven:
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>
但即使将此添加到我的POM后,此特定测试也会失败。
链接到解决方案
But even after adding this to my POM, this particulair test fails. Link to the solution
推荐答案
因此,经过几个小时的工作,我找到了问题的根本原因。
在Java 7u65中,它们使字节码验证器更加严格,字节码验证器正在读取PowerMock字节码并抛出VerifyError。
因此,基本上,PowerMock的字节代码没有传递字节码验证器。唯一的办法是将 -XX:-UseSplitVerifier
添加为VM参数或切换到Java 6.
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我的情况,当我删除 ruleRecord [1] .split(,)
时,当我实现自己的分割功能时,测试工作正常。我猜Powermock会在我们使用时弄乱字节码,比如像 whenNew()
这样的方法。
希望这有帮助。
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.
这篇关于arraylength中操作数堆栈上的错误类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!