0

I have this piece of code:

new Expectations(){
    {

    mFubar.getModel();
    result = new Model();
    times = 1;

    mFubar.getModel().getAllDogs();
    result = new HashSet<Dogs>();
    times = 1;
    }
};

Unfortunately I always get a null value for mFubar.getModel().
How can I create a mock value for getModel() so mFubar.getModel().getAllDogs(); works correctly?

4

1 回答 1

0

你得到一个 NPE,因为第二次调用mFubar.getModel(),就像第一次一样,返回null。您不能在期望记录块内使用记录的结果;这些值只能从被测代码中获得。

此外,这里的课程看起来并没有Model被嘲笑,所以尝试记录对的调用getAllDogs()也行不通。为此,您需要声明一个@Mocked Model model模拟字段或模拟参数。

最后,具有集合(List、Set、Map 等)作为其返回类型的模拟方法的默认返回值已经是一个空集合。result = new HashSet<Dog>();因此,无论如何,写作都是多余的。

于 2013-10-31T14:57:17.373 回答