0

我有一个类 FTPOperation,我用它作为基类来重新组合 FTP 操作常用的方法。其中一种方法是 connect()。

public abstract class FtpOperation {

    protected static final Log log = LogFactory.getLog(FtpOperation.class);

    /**
     * Hostname or IP address of the FTP server (e.g. localhost, 127.0.0.1).
     */
    private String hostName;

    private String username;

    private String password;

    protected FTPClient ftpClient = getFTPClient();

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Connect to the specified FTP server.
     * 
     * @throws Exception
     */
    protected void connect() throws Exception {
        int reply;

        // Connect to the FTP server
        ftpClient.connect(hostName);
        if (!ftpClient.login(username, password))
            throw new Exception("Fail to log in with the given credentials.");

        log.info("Connected to " + hostName + ".");
        log.info(ftpClient.getReplyString());

        // Check if the connection succeeded
        reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply))
            throw new Exception("Connection to FTP server failed with code "
                    + reply + ".");
    }

        /**
     * Used for mocking.
     * 
     * @return
     */
    protected FTPClient getFTPClient() {
        if (this.ftpClient == null)
            this.ftpClient = new FTPClient();
        return ftpClient;
    }
}

我想编写单元测试来测试这个方法,但我不知道如何测试它。我使用 Mockito 为 FTPClient 实例创建一个模拟对象。首先,我考虑过测试 ftpClient.connect() 调用返回某个异常的不同情况,但我认为这是错误的,因为我通过知道 connect() 方法的实现而不是通过 API 进行测试。在我所做的测试示例中:

@Test(expected = SocketException.class)
public void testConnectSocketException() throws Exception {
    downloadInitialFileTasklet.setHostName("hostname");
    doThrow(new SocketException()).when(mockFtpClient).connect("hostname");

    downloadInitialFileTasklet.connect();
}

有人可以向我解释测试这种方法的正确方法吗?

谢谢

4

2 回答 2

1

为 FtpClient 类创建一个接口,然后将其包装到您将在生产环境中使用的新类中。

对于测试,您可以实现一个存根(一个假类)或包装的 FtpClient 的模拟对象(我更喜欢第一种方式)。

将 IFtpClient 接口传递给 FtpOperation 类的构造函数。

于 2012-05-15T08:00:24.747 回答
1

你的测试意味着什么?如果您只是看到 SocketException 没有被捕获,那似乎是一个特殊的测试。

如果您要包装异常,那么它会更有意义。例如。

protected void connect() throws FTPException {
    int reply;

    // Connect to the FTP server
    try {
        ftpClient.connect(hostName);
    } catch (IOException e) {
        throw new FTPException(e, "unable to connect to: "+hostname);
    }
    ...
}

通过我们正在测试的测试,如果底层客户端无法连接,连接会提前终止并抛出 FTPException

@Test(expected = FTPException.class)
public void ConnectFailsIfExceptionOnClientConnect() throws FTPException {
    // setup
    downloadInitialFileTasklet.setHostName("hostname");
    when(mockFtpClient).connect(any(String.class)).doThrow(new SocketException());

    // verify -- if something else throws an FTP exception later then the verify
    // statements should fail the test because either connect was not called 
    // because or login was
    verify(mockFtpClient).connect(any(String.class));
    verify(mockFtpClient, never()).login(any(String.class), any(String.class));

    downloadInitialFileTasklet.connect();
}
于 2012-05-15T12:16:38.180 回答