不浪漫亦罪名

笼子里的金丝雀,笼子外的鸿鹄志

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)

posted @ 2020-08-25 11:26  不浪漫亦罪名  阅读(438)  评论(0编辑  收藏  举报