Arun Gowda
Arun Gowda

Reputation: 3500

Power mockito verify static calls real method

I am trying to verify a static method was never called while testing a service method with powerMockito 1.6.4

I followed This answer to do the same.

following is my code.

@RunWith ( PowerMockRunner.class)
@PrepareForTest ( MyClass.class)
@PowerMockIgnore ( "javax.net.ssl.*")
public class SomeTests
{
 @Test
    public void testMyMethodIsNotCalled() throws Exception
    {
        PowerMockito.mockStatic(MyClass.class);
        underTest.testMethod();
        PowerMockito.verifyStatic(Mockito.never());
        MyClass.myMethod(Mockito.any());
    }
}

The problem I am facing now is that, MyClass.myMethod(Mockito.any()); calls the real myMethod and gives a nullPointerException.

My assumption is that MyClass.myMethod(Mockito.any()); works with PowerMockito.verifyStatic(Mockito.never()); in order to specify the static method to be verified.

Am I missing something?

Upvotes: 0

Views: 313

Answers (1)

Amit Kumar Lal
Amit Kumar Lal

Reputation: 5789

you have to mock the static method behaviour also

i.e. something like this

PowerMockito.mockStatic(NameOfClass.class);
expect( NameOfClass.nameOfMethod((URL)Mockito.any(),Mockito.anyString())).andReturn(actualOutput);

refer Mock method with parameters

Upvotes: 1

Related Questions