Mocking behaviour resets after each test with PowerMock

The method PowerMockito.mockStatic(…) invokes MockCreator.mock(…). This method regsiters a Runnable that will be executed after each test : MockRepository.addAfterMethodRunner(new MockitoStateCleaner()); This runnable cleans the internal state of Mockito : private static class MockitoStateCleaner implements Runnable { public void run() { clearMockProgress(); clearConfiguration(); } private void clearMockProgress() { clearThreadLocalIn(ThreadSafeMockingProgress.class); } private void clearConfiguration() { clearThreadLocalIn(GlobalConfiguration.class); } … Read more

Powermock – java.lang.IllegalStateException: Failed to transform class

Powermock 1.6.3 uses javassist 3.15.2-GA which does not support certain types. Using 3.18.2-GA javassist worked for me. You may want to override dependency in your project. <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.2-GA</version> </dependency> You may face another problem for which the solution lies here Mockito + PowerMock LinkageError while mocking system class Hope this helps.

Unable to run JUnit test with PowerMockRunner

This is a bug that occurs when you use JUnit 4.12 and PowerMock < 1.6.1. The problem is solved in PowerMock 1.6.1. Please update your dependencies accordingly testCompile ‘junit:junit:4.12’, ‘org.powermock:powermock-core:1.6.1’, ‘org.powermock:powermock-module-junit4:1.6.1’, ‘org.powermock:powermock-api-mockito:1.6.1’ If you cannot upgrade PowerMock then you can use JUnit 4.11. testCompile ‘junit:junit:4.11’, ‘org.powermock:powermock-core:1.5.6’, ‘org.powermock:powermock-module-junit4:1.5.6’, ‘org.powermock:powermock-api-mockito:1.5.6’ Could you please add further lines of … Read more

PowerMock, mock a static method, THEN call real methods on all other statics

What are you looking for is called partial mocking. In PowerMock you can use mockStaticPartial method. In PowerMockito you can use stubbing, which will stub only the method defined and leave other unchanged: PowerMockito.stub(PowerMockito.method(StaticUtilClass.class, “someStaticMethod”)).toReturn(5); also don’t forget about the @PrepareForTest(StaticUtilClass.class)