Thomas De Rycke - 2013-09-26

Unitils FTP module

This unitils module makes it easy to use a fake FTP server during your tests.
A fake FTP server will be started before your unit test. You can configure it to point to a location on your filesystem.
Your application can do normal ftp calls to this location
Cleanup is done for you by the module.

This module makes use of MockFtpServer: http://mockftpserver.sourceforge.net/

FTP Module load artifact (maven)

    <dependency>
        <groupId>org.unitils.ftp</groupId>
        <artifactId>unitils-ftp</artifactId>
        <version>1.0.0</version>
    </dependency>

FTP Module project configuration

    unitils.modules=[..other modules..],ftp

    unitils.module.ftp.className = org.unitils.ftp.FtpModule  
    unitils.module.ftp.runAfter=  
    unitils.module.ftp.enabled=true

    org.unitils.ftp.baseFolder=src/test/resources/fakeFtpServerFolder  
    org.unitils.ftp.username=user  
    org.unitils.ftp.password=pass  
    org.unitils.ftp.port=6666

Example

@TestFtpServer
public FakeFtpServer server;

@Test
public void testDoSomething() throws IOException {

    //do some FTP calls.
    myApp.doSomeFtpThings();

    //verification
    server.getFileSystem().exists("file.txt");
}

So what happens behind this code:

Before each test the module checks if their is a field with @TestFtpServer. If this is true, than a fake FTP server is initialized with the properties out of the unitils.properties.
The username and the password are the credentials that are used to logon.
The filesystem of the fake server is imitated with the files in the basefolder.
After each test the server is stopped.
This runs best if the test and the SUT(System Under Test) is on the same JVM.
The test will fail if it is on a different machine.

 

Last edit: Willemijn Wouters 2013-10-16