FTP

Installation

If you are using maven, you can add following dependency to your project.

01
02
03
04
05
<dependency>
    <groupId>org.unitils.ftp</groupId>
    <artifactId>unitils-ftp</artifactId>
    <version>1.0.1</version>
</dependency>

Config

Please create unitils-local.properties, and add ftp to unitils.modules. Code as following:

01
02
03
04
05
06
07
08
09
10
unitils.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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
@RunWith(UnitilsJUnit4TestClassRunner.class)
public class FtpModuleOtherIntTest {
 
    @TestFtpServer(baseFolder="src/test/resources/otherFolder")
    public FakeFtpServer server;
 
 
    @Test
    public void getFile() throws IOException {
        //do some FTP calls.
 
        //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.