Powermock throws ClassNotPreparedException when using JUnit 5

Priya picture Priya · Aug 10, 2018 · Viewed 11.6k times · Source

I have a sample test class where I want to mock a static class.My build.gradle is like

testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.2.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.2.0'
testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.2.0'
testRuntime("org.junit.platform:junit-platform-launcher:1.1.1")

testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-beta.5'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.21.0'
testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.21.0'`

When I try my test case as

@ExtendWith(MockitoExtension.class)
@PrepareForTest(MyUtil.class)
public class MyTest {

@Test
public void shouldCheckIfSyncTimeIsAboveThreshold() {
    mockStatic(MyUtil.class);
    when(MyUtil.getValue()).thenReturn(5));
 }

But when I run this, I got exception like

org.powermock.api.mockito.ClassNotPreparedException:
The class MyUtil not prepared for test

Is there any way that I can achieve the same

Answer

Sam Brannen picture Sam Brannen · Aug 12, 2018

The MockitoExtension does not support Powermock.

Thus, there is no extension registered that would handle @PrepareForTest(MyUtil.class).

For details on how to use Powermock, visit the project webpage.