Mock File.createTempFile() and return our mocked File object

2k Views Asked by At

How can we mock File.createTempFile() to return our mocked File object?

It should return our mocked file object instead of creating a new temp file and return new object.

1

There are 1 best solutions below

3
On

This should do the trick

 PowerMockito.mockStatic(File.class);
 File mockFile =  PowerMockito.mock(File.class);
 when(File.createTempFile(anyString(), anyString())).thenReturn(mockFile);

cannot suggest more without the code or test class