try-with-resources testing with PowerMockito under static method

I will very much appreciate if I get any answer regarding my problem with static method test which holds try-with-resources part of code.

Static method:

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;
   }
}

And when I run my test, run-with-resources block makes testing impossible:

   @Test
   public void testStatic() {

          PowerMockito.mockStatic(PropertiesUtil.class);
          PowerMockito.when(PropertiesUtil.getProperties()).thenReturn(new Properties());

...
   }

I get failure trace:

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 ...

How can I test this with PowerMockito??? Thank you in advance!

Answers


Your test method not mocked properly. Below code snippet worked properly.

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

  }

}

OUTPUT:

output:{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;
    }
    
    @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); } }

Need Your Help

Stereo problems on Linux using Java 7 - how to change the GC of a component after it has been added to a parent

java opengl canvas awt stereo-3d

I have an AWT canvas contained within a JPanel and displayed on screen. I currently use Java 7 update 45 and run on Red Hat 6.

Sum elements of an array, what am I doing wrong?

java arrays

So I'm just trying to see if the elements of my array are divisible by the sum, I think something is wrong with my for-loop that sums up the elements of the array, any tips on how I should proceed?