Mockito asks to add @PrepareForTest for the class even after adding @PrepareForTest

Make sure you add @RunWith(PowerMockRunner.class) to the top of your class as well.

::edit:: two years later...

Don't ever use PowerMockito, you shouldn't need to.

If you do need to, you have most likely broken the SOLID principles and your design is wrong.

Fix your design instead.


For those trying to get this working with Junit 5, If your using the powermock-module-junit4 beta release which claims to be compatible with 4+, the library will still not recognize:

import org.junit.jupiter.api.Test;

and it will throw a:

org.powermock.api.mockito.ClassNotPreparedException

when @PrepareForTest is applied on the class you want to static mock. If you want to use PowerMock, you will have to go back to Junit 4 or create a MockWrapper for your static method at this time.

PowerMock 2.0: Github Roadmap


As with the last answer, my problem was also mixing the Test annotation from TestNG instead of Junit Test.

import org.junit.Test; // works

import org.testng.annotations.Test // did not work

Very abstruse error and I spent more than 5 hrs debugging :(