Mockito bypass static method for testing

后端 未结 3 1833
北海茫月
北海茫月 2020-12-17 16:15

I need to test handleIn() method using Mockito.

However the code need to call this legacy code Util.getContextPDO which is a static method.

Note that in test

3条回答
  •  囚心锁ツ
    2020-12-17 16:47

    when(handler2.getIPDO()).thenReturn(pdo);
    

    Will actually call the method and then return pdo regardless.

    Whereas:

    doReturn(pdo).when(handler2).getIPDO();
    

    Will return pdo without calling the getIPDO() method.

提交回复
热议问题