我有一个示例测试类,我想在其中模拟一个静态类。我的 build.gradle 就像
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'`
当我尝试我的测试用例时
@ExtendWith(MockitoExtension.class)
@PrepareForTest(MyUtil.class)
public class MyTest {
@Test
public void shouldCheckIfSyncTimeIsAboveThreshold() {
mockStatic(MyUtil.class);
when(MyUtil.getValue()).thenReturn(5));
}
但是当我运行它时,我遇到了异常
org.powermock.api.mockito.ClassNotPreparedException:
The class MyUtil not prepared for test
有什么办法可以达到同样的效果