Testing 尝试在静态方法下使用PowerMockito进行资源测试
如果我能得到任何关于静态方法测试问题的答案,我将不胜感激。静态方法测试包含代码的try-with-resources部分 静态方法:Testing 尝试在静态方法下使用PowerMockito进行资源测试,testing,junit,static,powermockito,Testing,Junit,Static,Powermockito,如果我能得到任何关于静态方法测试问题的答案,我将不胜感激。静态方法测试包含代码的try-with-resources部分 静态方法: public class PropertiesUtil { public static Properties getProperties() { Properties p = new Properties(); File configFile = new File("fileName");
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;
}
}
我得到了失败的痕迹:
java.lang.VerifyError: Inconsistent stackmap frames at branch target 663
Exception Details:
Location:
PropertiesUtil.getProperties()Ljava/util/Properties; @663: aload
Reason:
Type 'java/lang/Throwable' (current frame, locals[8]) is not assignable to 'java/lang/Class' (stack map, locals[8])
Current Frame:
bci: @653
flags: { }
locals: { 'java/util/Properties', top, 'java/lang/Throwable', 'java/lang/Throwable', top, 'java/lang/Throwable', 'java/lang/Throwable', top, 'java/lang/Throwable' }
stack: { }
Stackmap Frame:
bci: @663
flags: { }
locals: { 'java/util/Properties', top, 'java/lang/Throwable', top, top, 'java/lang/Throwable', 'java/lang/Throwable', top, 'java/lang/Class' }
stack: { }
Bytecode:
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 ...
java.lang.VerifyError:分支目标663处的堆栈映射帧不一致
例外情况详情:
地点:
PropertiesUtil.getProperties()Ljava/util/Properties@663:aload
原因:
类型“java/lang/Throwable”(当前帧,局部变量[8])不可分配给“java/lang/Class”(堆栈映射,局部变量[8])
当前帧:
bci:@653
标志:{}
本地语言:{java/util/Properties',top',java/lang/Throwable',java/lang/Throwable',top',java/lang/Throwable',java/lang/Throwable',top',java/lang/Throwable'}
堆栈:{}
堆栈映射帧:
密件抄送:@663
标志:{}
局部变量:{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测试它???
提前谢谢你 您的测试方法没有正确模拟。下面的代码片段工作正常
@RunWith(PowerMockRunner.class)
@PrepareForTest(PropertiesUtil.class)
public class PropertiesUtilTest {
@Before
public void setUp() throws Exception{
Properties mockProps = new Properties();
mockProps.put("user", "kswaughs");
PowerMockito.mockStatic(PropertiesUtil.class);
PowerMockito.when(PropertiesUtil.getProperties()).thenReturn(mockProps);
}
@Test
public void testProps() throws Exception{
Properties p = PropertiesUtil.getProperties();
System.out.println("output:"+ p);
}
}
输出:
输出:{user=kswaughs}
您的测试方法没有正确模拟。下面的代码片段工作正常
@RunWith(PowerMockRunner.class)
@PrepareForTest(PropertiesUtil.class)
public class PropertiesUtilTest {
@Before
public void setUp() throws Exception{
Properties mockProps = new Properties();
mockProps.put("user", "kswaughs");
PowerMockito.mockStatic(PropertiesUtil.class);
PowerMockito.when(PropertiesUtil.getProperties()).thenReturn(mockProps);
}
@Test
public void testProps() throws Exception{
Properties p = PropertiesUtil.getProperties();
System.out.println("output:"+ p);
}
}
输出:
输出:{user=kswaughs}
- mockito all-1.10.19
- powermock-module-junit4-1.6.5
- powermock api mockito-1.6.5
- junit-4.12 public class PropertiesUtil { private static Properties configProperties; public static Properties getConfigProperties() { if (configProperties == null) { try (InputStream resourceAsStream = PropertiesUtil.class.getClassLoader() .getResourceAsStream("config.properties")) { Properties properties = new Properties(); if (resourceAsStream != null) { properties.load(resourceAsStream); configProperties = properties; } } catch (IOException e) { e.printStackTrace(); } } return configProperties; } } @试验 公共void testUtil(){ Properties prop=PropertiesUtil.getConfigProperties(); 系统错误打印项次(道具); } }
- mockito all-1.10.19
- powermock-module-junit4-1.6.5
- powermock api mockito-1.6.5
- junit-4.12 public class PropertiesUtil { private static Properties configProperties; public static Properties getConfigProperties() { if (configProperties == null) { try (InputStream resourceAsStream = PropertiesUtil.class.getClassLoader() .getResourceAsStream("config.properties")) { Properties properties = new Properties(); if (resourceAsStream != null) { properties.load(resourceAsStream); configProperties = properties; } } catch (IOException e) { e.printStackTrace(); } } return configProperties; } } @试验 公共void testUtil(){ Properties prop=PropertiesUtil.getConfigProperties(); 系统错误打印项次(道具); } }