模拟输入是列表的方法

时间:2014-07-14 14:24:32

标签: java mockito

我有以下代码:

    sshCPList.add(new SSHParameter("ssh root@io1", "Password:", "(yes/no)?"));
    sshCPList.add(new SSHParameter(rootpwd, rootPrompt, null));
    sshCPList.add(new SSHParameter("ssh root@io1", "Password:", "(yes/no)?"));
    sshCPList.add(new SSHParameter(rootpwd, rootPrompt, null));
    if(!ssh2.sendSshShellCommandToUnknownHost(sshCPList)){
        theLogger.error("Failed to Authorize the PMF function to login as root");
        result = false;
    }

我想定义以下内容:

when(ssh2.sendSshShellCommandToUnknownHost(*a specific List which contains the four SSHParameter object or has 4 items or sonmething*).thenReturn(false);

问题是我无法将List定义为正确的输入。

有任何建议或解决方案吗?

2 个答案:

答案 0 :(得分:2)

您可以使用eq(T value),其中value是该特定列表。

类似的东西:

List<SSHParameter> expectedList = prepareExpectedList();
when(ssh2.sendSshShellCommandToUnknownHost(eq(expectedList)).thenReturn(false) 

prepareExpectedList();会向您要在测试中提供的输入返回equals的列表。

请参阅http://docs.mockito.googlecode.com/hg/latest/org/mockito/Matchers.html

答案 1 :(得分:2)

要生成自定义Matcher,您可以使用:

private static class MyListMatcher extends ArgumentMatcher<List<SSHParameter>> {

    private List<SSHParameter> expectedList;

    private MyListMatcher (List<SSHParameter> expectedList) {
        this.expectedList= expectedList;
    }

    @Override
    public boolean matches(Object obj) {
        List<SSHParameter> listToMatch = (List<SSHParameter>) obj;
        boolean result = true;// check whatever you want between listToMatch and expectedList (size, elements etc.);
        return result;
    }
}

并使用它:

 when(ssh2.sendSshShellCommandToUnknownHost(argThat(new MyListMatcher(expectedList))).thenReturn(false);