【发布时间】:2016-09-16 16:36:50
【问题描述】:
如果我得到任何关于我的静态方法测试问题的答案,我将非常感激,该测试包含代码的 try-with-resources 部分。
静态方法:
public class PropertiesUtil {
public static Properties getProperties() {
Properties p = new Properties();
File configFile = new File("fileName");
<b>try (FileReader pFile = new FileReader(configFile)) {</b>
p.load(pFile);
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
}
当我运行我的测试时,run-with-resources 块使测试变得不可能:
@Test
public void testStatic() {
PowerMockito.mockStatic(PropertiesUtil.class);
PowerMockito.when(PropertiesUtil.getProperties()).thenReturn(new Properties());
...
}
我得到失败跟踪:
java.lang.VerifyError:分支目标 663 处的堆栈图帧不一致 异常详情: 地点: PropertiesUtil.getProperties()Ljava/util/Properties; @663:加载 原因: 类型“java/lang/Throwable”(当前帧,locals[8])不可分配给“java/lang/Class”(堆栈映射,locals[8]) 当前帧: 密送:@653 标志:{} locals: { 'java/util/Properties', top, 'java/lang/Throwable', 'java/lang/Throwable', top, 'java/lang/Throwable', 'java/lang/Throwable', top, ' java/lang/Throwable' } 堆: { } 堆栈图框架: 密送:@663 标志:{} locals: { 'java/util/Properties', top, 'java/lang/Throwable', top, top, 'java/lang/Throwable', 'java/lang/Throwable', top, 'java/lang/Class' } 堆: { } 字节码: 0000000: 123d b800 4312 4403 bd00 0312 46b8 004a 0000010: 124c b800 524b 2a01 3a05 013a 0619 0512 0000020: 59b8 005b 125c 125d b800 60b8 0064 3a07 0000030: 1907 b200 56a6 000b b200 563a 06a7 000a 0000040: 1907 c000 653a 0619 06a5 0008 2ac0 0057 0000050: b000 0000 0001 3a05 013a 0612 66b8 0060 0000060:03bd 0003 1267 b800 68b8 006c 3a07 1907 0000070: b200 56a5 0032 1907 c100 6e99 0020 b800 0000080: 7412 75b8 0060 1203 01b6 007b b600 7f01 ...如何使用 PowerMockito 进行测试??? 提前谢谢!
【问题讨论】:
标签: testing junit static powermockito