该方法将
PowerMockito.mockStatic(...)调用
MockCreator.mock(...)。此方法注册一个Runnable,它将
在每次测试后 执行:
MockRepository.addAfterMethodRunner(new MockitoStateCleaner());
这个可运行的清理 Mockito 的 内部状态 :
private static class MockitoStateCleaner implements Runnable { public void run() { clearMockProgress(); clearConfiguration(); } private void clearMockProgress() { clearThreadLocalIn(ThreadSafeMockingProgress.class); } private void clearConfiguration() { clearThreadLocalIn(GlobalConfiguration.class); } private void clearThreadLocalIn(Class<?> cls) { Whitebox.getInternalState(cls, ThreadLocal.class).set(null); final Class<?> clazz = ClassLoaderUtil.loadClass(cls, ClassLoader.getSystemClassLoader()); Whitebox.getInternalState(clazz, ThreadLocal.class).set(null); }}
因此,您应该在每次测试之前执行setUp。
@Beforepublic void setUp() { PowerMockito.mockStatic(A.class); PowerMockito.when(A.getVal()).thenReturn("X"); b = PowerMockito.mock(B.class); PowerMockito.when(b.getVal()).thenReturn("Y");}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)