簡體   English   中英

即使在存根之后,Mockito 仍然返回 null

[英]Mockito keep returning null even after stub

我有這樣的結構

...
SomeDao dao = getDAO(AttributeDAO.class);
CustomType type =dao.findByKey(typeOne, typeTwo, typeThree.toString());
if(type == null) {
    System.out.print("null returned");
} else {
    System.out.print("do something");
}
...

我的測試用例

...
        MainClass mc = Mockito.spy(new MainClass());
        CustomType type = new CustomType();
        SomeDao dao = Mockito.mock(AttributeDAO.class);
        type.setValueOne(1);
        type.setValueTwo(1);
        type.setValueThree("Y");
        Mockito.doReturn(type).when(dao).findByKey(1,2,"Y");

        mc.callThisDaoFunction();
...

但是每當我嘗試使用 eclemma 覆蓋工具返回類型時,它一直說type == null我如何我的測試用例或設置類型以返回非空值?

您需要將dao模擬注入MainClass 否則在調用getDao(AttributeDAO.class)時它仍然會嘗試檢索真正的dao

  • 如果有一個方法setDao它只會用你的模擬調用它。
  • 否則,當使用參數AttributeDAO.class調用mc時返回dao

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM