Forrie
Forrie

Reputation: 73

Return same value for every instance of a method call

Hi I'm not using PowerMockito but normal one and trying to mock something like this:

when(any(File.class).canWrite()).thenReturn(Boolean.FALSE)

But I get a NullPointerException. Basically without mocking a specific instance I want to mock any and all instances of a file object to return FALSE for canWrite().

Can anyone help? I can mock the object but the code I'm testing is inside a static method.

Upvotes: 3

Views: 2601

Answers (1)

Jason D
Jason D

Reputation: 8673

This is not possible. With regular Mockito you need some mock object in the when() call, not an any matcher.

For your example, when you say any(File.class)

when(any(File.class).canWrite()).thenReturn(Boolean.FALSE)

You need to have a file object already instantiated as a Mock

File fileMock = mock(File.class);    
when(fileMock.canWrite()).thenReturn(Boolean.FALSE)

Upvotes: 2

Related Questions