未捕获或未抛出预期异常?
我正在测试一个允许连接到 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");
}
}
我想测试当 serverAddress 无效时 connectFTP() 方法是否抛出异常。
这是我的测试:
@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;
}
目前,测试不起作用。 感谢您的帮助!
I am testing a function that permit to connect to FTP server.
Here is one of my test who works properly:
@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");
}
}
I want to test if the connectFTP() method throw an exception when the serverAddress is invalid.
Here is my test:
@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);
}
Here is my function:
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;
}
Presently, the test doesn't work.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试未通过的原因是它期望抛出异常,但异常在“connectFTP”方法中被捕获,然后返回 false。
连接失败时返回 false 还是抛出异常取决于代码的语义。根据布尔返回值,您似乎期望在出现异常时返回 false。在这种情况下,您需要
在 @Test 注释中执行而不是使用 (expected = Exception.class) 。
The reason the test is not passing is that it is expecting an Exception to be thrown, but the exception is being caught inside the 'connectFTP' method, which is then returning false.
Whether you return false when connection fails or throw an exception depends on the semantics of your code. Based on the boolean return value, it seems like you're expecting false to be returned when there is an exception. In that case you'll want to do
instead of using (expected = Exception.class) in the @Test annotation.
看起来您自己在代码中捕获了异常
如果你从外部调用“connectFTP”方法(无论是否是junit,它都不会抛出异常。
这就是您的 JUnit 无法工作的原因。
顺便说一句,最好不要直接使用 Exception,而是使用与您的情况相关的子类型。
it looks like you're catching an exception by yourself in your code
If you call the method 'connectFTP' from outside (it doesn't matter whether its a junit or not, it just won't throw an exception.
That's why your JUnit doesn't work.
BTW, it would be better not to work directly with Exception, but with its subtype relevant for your case.