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.
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.
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.
If you want to test is the username and password is invalid, perhaps you can just set the FTP reply code to 430:-
I understand you wanted to test the
SocketExceptionexception thrown bySocketClient.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 getSocketExceptionthrown here, but I would think the behavior is close enough to the actual implementation code.