java - 如何让 MockFTPServer 抛出异常

标签 java unit-testing ftp mocking

我有一些连接到 FTP 服务器的代码,我正在尝试为该代码编写测试用例。为此,我一直在尝试使用 MockFtpServer 来模拟 FTP 服务器,以便测试我的交互。

http://mockftpserver.sourceforge.net/index.html

在一个特定的案例中,我试图用一个看起来像这样的测试用例来测试我的“连接”方法:

public class FTPServiceTestWithMock {

    private FakeFtpServer server;
    private FTPService service;
    private int controllerPort;

    @Before
    public void setup() {
        server = new FakeFtpServer();
        server.setServerControlPort(0); // Use free port - will look this up later

        FileSystem fileSystem = new WindowsFakeFileSystem();
        fileSystem.add(new DirectoryEntry("C:\\temp"));
        fileSystem.add(new FileEntry("C:\\temp\\sample.txt", "abc123"));
        server.setFileSystem(fileSystem);
        server.addUserAccount(new UserAccount("user", "pass", "C:\\"));
        server.start();
        controllerPort = server.getServerControlPort();

        service = new FTPService();
    }

    @After
    public void teardown() {
        server.stop();
    }

    @Test
    public void testConnectToFTPServer() throws Exception {
        String testDomain = "testdomain.org";
        String expectedStatus = 
            "Connected to " + testDomain + " on port " + controllerPort;

        assertEquals(
            expectedStatus, 
            service.connectToFTPServer(testDomain, controllerPort)
        );
    }

}

此代码完美运行 - 它设置了一个伪造的 FTP 服务器并测试我的代码以确保它可以连接并返回适当的消息。

但是,我的 FTP 客户端的 API 规范显示当我尝试连接时会抛出异常。

http://commons.apache.org/net/api-3.1/org/apache/commons/net/SocketClient.html#connect%28java.lang.String%29

我想编写第二个测试用例来测试抛出的异常,这很可能是域名不正确或 FTP 服务器已关闭。我想确保在这种情况下,我的软件能够做出适当的响应。我在模拟 FTP 服务器站点上找到了有关“自定义命令处理程序”的信息,但我不知道如何使它抛出异常。这是我的:

public void testConnectToFTPServerConnectionFailed() throws Exception {
    ConnectCommandHandler connectHandler = new ConnectCommandHandler();
    connectHandler.handleCommand(/* Don't know what to put here */);
    server.setCommandHandler(CommandNames.CONNECT, connectHandler);
}

handleCommand 方法需要一个 Command 对象和一个 Session 对象,但我无法从文档中弄清楚如何获取要发送的有效对象。有人知道如何处理吗?

谢谢。

最佳答案

既然没有人在这里试一试,我就试一试。我以前从未使用过 MockFtpServer,所以这是我的第一次尝试。

在我看来,测试服务器是否已关闭的最简单方法就是关闭服务器。

@Test(expected = IOException.class)
public void testConnectToFTPServer_ServerDown() throws Exception {
    // kill the server
    server.stop();
    service.connectToFTPServer("testdomain.org", controllerPort);
}

如果你想测试用户名和密码是否无效,也许你可以将 FTP 回复代码设置为 430:-

@Test(expected = IllegalStateException.class)
public void testConnectToFTPServer_InvalidUserPassword() throws Exception {
    // assuming you already started it in setup(), you may want to stop it first
    if (server.isStarted()) {
        server.stop();
    }

    ConnectCommandHandler connectCommandHandler = (ConnectCommandHandler) server.getCommandHandler(CommandNames.CONNECT);

    // 430 = FTP error code for "Invalid username or password"
    connectCommandHandler.setReplyCode(430);
    server.setCommandHandler(CommandNames.CONNECT, connectCommandHandler);
    server.start();

    service.connectToFTPServer("testdomain.org", controllerPort);
}

我知道您想测试 SocketClient.connect() 抛出的 SocketException 异常……而这个异常在这里有点棘手。基于FTP error codes ,我相信您正在寻找 10000 系列错误代码之一(如果我在这里错了请纠正我,但我不是 FTP 专家)。这个 10000 系列错误代码的问题是它会使您的测试无限期地旋转,因为该错误代码指示无法连接远程服务器,因此在针对 MockFtpServer 进行测试时,测试并不知道何时停止。所以,在这种情况下,我将测试设置为 5 秒后超时(我认为这是合理的)。当然,您不会在此处抛出 SocketException,但我认为该行为与实际实现代码足够接近。

@Test(timeout = 5000)
public void testConnectToFTPServer_InvalidHostName() throws Exception {
    // assuming you already started it in setup(), you may want to stop it first
    if (server.isStarted()) {
        server.stop();
    }

    ConnectCommandHandler connectCommandHandler = (ConnectCommandHandler) server.getCommandHandler(CommandNames.CONNECT);

    // 10060 = FTP error code for "Cannot connect to remote server."
    connectCommandHandler.setReplyCode(10060);
    server.setCommandHandler(CommandNames.CONNECT, connectCommandHandler);
    server.start();

    service.connectToFTPServer("testdomain.org", controllerPort);
}

关于java - 如何让 MockFTPServer 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13243039/

相关文章:

基于Java的用户账户管理系统

ruby - 基于条件的 Rspec 测试

Java Apache FTP 客户端 - 恢复损坏的上传

linux - 无法解压文件?

java - NIC 吞吐量样本

Java - 使用 JNA 的 Windows 任务栏 - 如何将窗口图标 (HICON) 转换为 Java 图像?

java - 使用 <c :import> with spring views 的可能性

unit-testing - 使用自定义属性编辑器时如何对 Grails Controller 进行单元测试?

javascript - Grunt Karma 运行测试但阻止

PHP - 从远程服务器获取 ftp 列表的最佳做法是什么?