在静态方法下使用PowerMockito进行资源测试

时间:2016-09-16 16:36:50

标签: testing junit static powermockito

如果我对静态方法测试的问题得到任何答案,我会非常感激。静态方法测试包含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: 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 ...

如何使用PowerMockito测试? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

您的测试方法没有正确模拟。下面的代码片段工作正常。

@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);

  }

}

<强>输出:

  

输出:{用户= kswaughs}

答案 1 :(得分:0)

  • 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;
    }
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({PropertiesUtil.class})
    public class PropertiesUtilTest {
    
    @Before
    public void setUp(){
    
    
    Properties mockProperties = new Properties();
    mockProperties.setProperty("propertyKey", "propertyValue");
    
    PowerMockito.mockStatic(PropertiesUtil.class);
        PowerMockito.when(PropertiesUtil.getConfigProperties()).thenReturn(mockProperties);
    
    } @Test public void testUtil(){ Properties prop = PropertiesUtil.getConfigProperties(); System.err.println(prop); } }