Fernando Garcia
Fernando Garcia

Reputation: 1969

Why when mocking a private method using Powermock is calling the real method?

I mock a method using a line like this in the @Before test method:

PowerMockito.when(mockedObject, method(MockedClass.class, "methodIWantToMock")).
            withArguments(Matchers.anyString()).thenReturn("AnyExpectedResult");

I tried this as well:

Whitebox.invokeMethod(mockedObject, "methodIWantToMock", "AnyExpectedResult");

I mock it because I don't want to execute it, but when those lines are executed, the real method is called, and I want to avoid that. I don't know why it is happening and how to avoid it. I couldn't find any question like this one in the site and internet. Any ideas? Thanks.

Upvotes: 2

Views: 725

Answers (1)

Fernando Garcia
Fernando Garcia

Reputation: 1969

Just realized that to do that I need to add the class I want to mock inside the annotation @PrepareForTest.

Upvotes: 2

Related Questions