如何模拟内部调用的方法



下面是我正在尝试为其编写文本大小写的代码,并添加了我所做的但得到空指针 exp

public boolean doVersionLimitCheck(Long mneId) throws DMMException {
    CALogUtil.getInstance().logMethodEntry("doVersionLimitCheck",
            ConfigArchiveManagerImpl.class.getName());
    boolean status = false;
    status = validateArchivedVersions(mneId);
    CALogUtil.getInstance().logDebug("Version Roll over status::" + status);
    CALogUtil.getInstance().logMethodExit("doVersionLimitCheck",
            ConfigArchiveManagerImpl.class.getName());
    return status;
}

为此,我喜欢以下内容。

@Test
public void testDoVersionLimitCheck() {
    Long mneId=Long.valueOf("123");
    ConfigArchiveManagerImpl impl = new ConfigArchiveManagerImpl();
    try {
        Mockito.doReturn(true).when(Mockito.mock(ConfigArchiveManagerImpl.class)).validateArchivedVersions(Mockito.anyLong());
    } catch (DMMException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        impl.doVersionLimitCheck(mneId);
    } catch (DMMException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

您需要监视 SUT 以测试一种方法并模拟另一种方法:

@Test
public void testDoVersionLimitCheck() {
    Long mneId=Long.valueOf("123");
    ConfigArchiveManagerImpl impl = Mockito.spy(new ConfigArchiveManagerImpl());
    try {
        Mockito.doReturn(true).when(impl ).validateArchivedVersions(Mockito.anyLong());
    } catch (DMMException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

相关内容

  • 没有找到相关文章