PowerMockito 静态方法 单例工厂方式
IdentityLogFactory mockInstance = Mockito.mock(IdentityLogFactory.class); PowerMockito.whenNew(IdentityLogFactory.class).withNoArguments().thenReturn(mockInstance); PowerMockito.mockStatic(IdentityLogFactory.class); PowerMockito.when(IdentityLogFactory.getInstance()).thenReturn(mockInstance); PowerMockito.when(mockInstance.getLogHandler("USABLE")).thenReturn(usableLogHandler); PowerMockito.when(usableLogHandler.search(Mockito.any())).thenReturn(new BaseResult<Page>()); BaseResult baseResult = identityLogController.doPost(queryForm); Assert.assertNotNull(baseResult);
测试类上添加:
@RunWith(PowerMockRunner.class)
@PrepareForTest(IdentityLogFactory.class)