Knot
Knot

Reputation: 141

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

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.

Upvotes: 2

Views: 2364

Answers (1)

pvpkiran
pvpkiran

Reputation: 27048

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

Upvotes: 1

Related Questions