我正在尝试使用PowerMock测试一个方法。目前还没有编写任何测试用例,只是在为模拟设置类。下面是目前的进展:
@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 {
}
}
当我尝试运行这个测试时,我会收到以下错误信息。
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' }
在cmd上运行“java -version”命令会显示版本号为1.7.0_65。
“java_home”环境变量被设置为C:\Program Files\Java\jdk1.7.0_65。
在eclipse中,我正在使用jre 7,编译器级别为1.7。
在我的maven中,我添加了以下依赖项以允许接受字符串而不是字符的switch语句:
<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>
有人能帮我解决这个问题吗?
下面是我正在测试的方法:
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;
}
我在网上找到了解决方案:它说当我运行测试时,需要将 -XX:-UseSplitVerifier
添加到 VM 参数中。这样做后,测试可以运行。但我需要让它与 Maven 一起运行,为此,解决方案建议我向 Maven 添加以下插件:
The solution online suggests adding 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中添加了这个依赖项,这个特定的测试仍然失败。 解决方案链接