gpt4 book ai didi

java - 这种情况下如何通过API进行单元测试呢?

转载 作者:行者123 更新时间:2023-12-02 07:47:47 26 4
gpt4 key购买 nike

我有一个类 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();
}

有人可以向我解释一下测试此方法的正确方法吗?

谢谢

最佳答案

您的测试要测试什么?如果您只是看到 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();
}

关于java - 这种情况下如何通过API进行单元测试呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com