返回false时,PowerMock不存根

时间:2015-11-14 23:52:26

标签: java unit-testing testng powermockito

我有这个函数来创建一个模拟的Category类。

@Mock Category child;
@Mock OwnableHelper ownableHelper;

private Category getTreeRoot(boolean isOwnedBy) {

    Category category = mock(Category.class);
    when(category.getChildren()).thenReturn(new RealmList<>(child));

    when(ownableHelper.isOwnedBy(any(Ownable.class), any(HasId.class))).thenReturn(isOwnedBy);

    return treeRoot;
}

测试是:

@Test(enabled = true)
public void RecursiveCallOnChildWithoutOwnOwnable() throws Exception {
    Category parent = getTreeRoot(false);
    ownableUpdater.updateRecursive(parent);
    verify(ownableUpdater).updateRecursive(child);
}

@Test(enabled = true)
public void DontCallRecursiveWhenChildHasOwnOwnable() throws Exception {
    Category parent = getTreeRoot(true);
    ownableUpdater.updateRecursive(parent);
    verify(ownableUpdater, never()).updateRecursive(child);
}

现在我想测试一下这段代码:

    for (Category child : category.getChildren()) {
        if (!ownableHelper.isOwnedBy(owner, child))
            updateRecursive(child);
    }

当我使用ownableHelper.isOwnedBy()存储true时,测试会完成它应该做的事情。但是,如果我通过false category.getChildren()会抛出java.lang.NullPointerException

java.lang.NullPointerException
at OwnableUpdater.updateRecursive(OwnableUpdater.java:72)
at RecursiveUpdaterTest.RecursiveCallOnChildWithoutOwnOwnable(RecursiveUpdaterTest.java:93)

,第72行是for (Category child : category.getChildren())

如何将某些内容存根为false而不是true会使另一个存根失败?

我使用testng 6.1.1来运行我的测试,测试从PowerMockTestCase扩展。

0 个答案:

没有答案