跳过Mockito中的内部方法

时间:2016-06-23 18:45:51

标签: java unit-testing mocking atg

我正在尝试使用mockito测试以下方法。我想在执行getProfileItemFinder().retrieveEmailRecipient(email)时跳过class.getUserWishList("abc@gmail.com")方法调用;

我已尝试过以下代码,但每当我尝试运行代码时,我都会在doReturn处获得mockito异常。你能否告诉我我做错了什么。

示例模拟方法:

profileItemFinder = spy(new TestProfileItemFinder());
doReturn(profileItem).when( profileItemFinder.retrieveEmailRecipient(email));

原创方法:

public List<Map<String, Object>> getUserWishList(String email) {
    // JSONArray profileGiftList = new JSONArray();
    ---------------------------------------------
    -----------------------------------------------
    if (email != null) {

        RepositoryItem profileItem = getProfileItemFinder().retrieveEmailRecipient(email);

        if (profileItem != null) {
      ----------------------------------
      --------------------------------

    }

            }
        }
    }

    ---------------------------

}

1 个答案:

答案 0 :(得分:0)

假设上面正确定义了email和profileItem,现在代替:

doReturn(profileItem).when( profileItemFinder.retrieveEmailRecipient(email));

尝试下面并检查一次,

doReturn(profileItem).when( getProfileItemFinder().retrieveEmailRecipient(email));