使用JUnit 5时Powermock抛出ClassNotPreparedException [英] Powermock throws ClassNotPreparedException when using JUnit 5
本文介绍了使用JUnit 5时Powermock抛出ClassNotPreparedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个示例测试类,我想在其中模拟一个静态类.build.gradle就像
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'`
当我以
@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
推荐答案
MockitoExtension
不支持Powermock.
The MockitoExtension
does not support Powermock.
因此,没有注册可以处理@PrepareForTest(MyUtil.class)
的扩展名.
Thus, there is no extension registered that would handle @PrepareForTest(MyUtil.class)
.
有关如何使用Powermock的详细信息,请访问项目网页.
For details on how to use Powermock, visit the project webpage.
这篇关于使用JUnit 5时Powermock抛出ClassNotPreparedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文