我正在测试一个允许连接到FTP服务器的功能。预期的异常没有被捕获或不被抛出?
这里是我的测试中一个谁正常工作:
@Test
public void connectTestValid()
{
assetSource.setPassword("password");
assetSource.setUsername("user");
assetSource.setServerAddress("127.0.0.1");
assetSource.setServerPort(21);
connectionSuccess = false;
connectionSuccess = ftpFolderTest.connectFTP(ftpClient);
if (!connectionSuccess)
{
fail("Expected Connection success");
}
}
我要测试是否connectFTP()方法抛出一个异常时serveraddress为无效。
这里是我的测试:
@Test(expected = Exception.class)
public void connectTestInvalidServerAddress()
{
assetSource.setPassword("password");
assetSource.setUsername("user");
assetSource.setServerAddress("1");
assetSource.setServerPort(21);
connectionSuccess = false;
connectionSuccess = ftpFolderTest.connectFTP(ftpClient);
}
这里是我的功能:
protected boolean connectFTP(FTPClient ftp)
{
try
{
ftp.connect(getAssetSource().getServerAddress());
if (!ftp.login(getAssetSource().getUsername(), getAssetSource().getPassword()))
{
logger.error("Login Failed");
ftp.disconnect();
return connectionSuccess = false;
}// if
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode()))
{
logger.error("Connection Failed");
ftp.disconnect();
return connectionSuccess = false;
}// if
}
catch (Exception e)
{
e.printStackTrace();
return connectionSuccess = false;
}
return connectionSuccess = true;
}
目前,测试不起作用。 感谢您的帮助!
那么你是不是扔了'Exception' catch块,而不是返回false,重新抛出异常,否则就是吞下它。 – CoolBeans 2012-01-31 18:16:59