mercredi 30 mars 2016

PowerMock mockStatic not mocking the class

I have the following class(and method in it)

class Fetcher{

 public void fetch(String key){
      File file = File.createTempFile(key,"*.txt");
      ..... 
      ....
 } 

}

I want to unit test this method and want to mock the createTempFile method For this i have written the unit test as follows

@RunWith(PowerRunner.class)

@PrepareForTest({File.class})

public class FetcherTest {

public void test() {
 String key = "key";
 File file = new File("Hello");
 PowerMock.mockStatic(File.class);
 EasyMock.expect(File.createTempFile(EasyMock.anyObject(String.class),EasyMock.anyObject(String.class))).andReturn(file).once();
 PowerMock.replay(File.class);
 Fetcher fetcher = new Fetcher();
 fetcher.fetch("key");
 PowerMock.verify(File.class);

} }

Executing the unit test provides the following error:

Expectation failure on verify: File.createTempFile(,): expected: 1,actual: 0

I have looked through a lot of articles but am not able to figure out what's missing here and why File is not getting mocked. Please help with any suggestions

Aucun commentaire:

Enregistrer un commentaire