Powermock throws ClassNotPreparedException when using JUnit 5

て烟熏妆下的殇ゞ 提交于 2019-12-13 04:22:55

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/51779581/powermock-throws-classnotpreparedexception-when-using-junit-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!