Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8794247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:13:34+00:00 2026-06-13T23:13:34+00:00

I have some code that connects to an FTP server and I’m trying to

  • 0

I have some code that connects to an FTP server and I’m trying to write test cases for that code. In doing that, I’ve been trying to use MockFtpServer to mock out the FTP server so I can test my interactions.

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

In one particular case, I’m trying to test my “connect” method with a test case that looks something like this:

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)
        );
    }

}

This code works perfectly – it sets up a fake FTP server and puts my code under test to make sure it can connect and returns an appropriate message.

However, the API spec for my FTP client shows that exceptions can be thrown when I try to connect.

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

I would like to write a second test case that tests for an exception being thrown, which is likely should the domain name be incorrect or the FTP server is down. I want to ensure that, in such a case, my software responds appropriately. I found information in the Mock FTP Server site about “custom command handlers”, but I can’t figure out how to make one throw an exception. This is what I have:

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

The handleCommand method requires a Command object and a Session object, but I can’t figure out, from the documentation, how to get valid objects to send in. Does anyone know how to go about this?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:13:35+00:00Added an answer on June 13, 2026 at 11:13 pm

    Since no one is taking a stab here, I’m going to give it a shot. I have never used MockFtpServer before, so this is my first try.

    The easiest way in my opinion to test if the server is down is to just shut down the server.

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

    If you want to test is the username and password is invalid, perhaps you can just set the FTP reply code to 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);
    }
    

    I understand you wanted to test the SocketException exception thrown by SocketClient.connect()… and this one gets a little tricky here. Based on the FTP error codes, I believe you are shooting for one of the 10000 series error code (correct me if I’m wrong here, but I’m no expert in FTP). The problem with this 10000 series error code is it will make your test spins indefinitely simply because that error code dictates the remote server cannot be connected, thus the test doesn’t really know when to stop when testing against MockFtpServer. So, in this case, I set the test to timeout in 5 seconds (which I think is reasonable). Sure, you will not get SocketException thrown here, but I would think the behavior is close enough to the actual implementation code.

    @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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code I need to write a test for that connects to
I have some Java code that connects to an Oracle database using DriverManager.getConnection(). It
I have some code that uses the SQL Server 2005 SMO objects to backup
When should you throw a custom exception? e.g. I have some code that connects
I have the following C# code that connects to my domain server and performs
I have some code that will change the background color of a specific label
I have some code that is supposed to return an NSString. Instead it is
I have some code that generates Visio masters for me, and some masters have
I have some code that causes the box2d physics simulation to stutter forever after
I have some code that runs a model in a loop. Each iteration of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.