未捕获或未抛出预期异常?

发布于 2025-01-01 22:37:58 字数 1669 浏览 2 评论 0原文

我正在测试一个允许连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半山落雨半山空 2025-01-08 22:37:58

测试未通过的原因是它期望抛出异常,但异常在“connectFTP”方法中被捕获,然后返回 false。

连接失败时返回 false 还是抛出异常取决于代码的语义。根据布尔返回值,您似乎期望在出现异常时返回 false。在这种情况下,您需要

org.junit.Assert.assertFalse(connectionSuccess); 

在 @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

org.junit.Assert.assertFalse(connectionSuccess); 

instead of using (expected = Exception.class) in the @Test annotation.

幽蝶幻影 2025-01-08 22:37:58

看起来您自己在代码中捕获了异常
如果你从外部调用“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文