Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Spring Integration FakeFtpServer Example

4.00/5 (1 vote)
24 Dec 2012CPOL 12.1K  
In this blog, we will test a Spring Integration flow which checks for a list of files, applies a splitter to separate each file, and starts downloading them into a local location.

For people in a hurry, get the latest code and the steps in GitHub.

To run the junit test, run “mvn test” and understand the test flow.

Introduction: FakeFtpServer

In this Spring Integration FakeFtpServer example, I will demonstrate using Spring FakeFtpServer to JUnit test a Spring Integration flow. This is an interesting topic, there are a few articles like Unit testing file transfers, which give some insight on this topic.

In this blog, we will test a Spring Integration flow which checks for a list of files, applies a splitter to separate each file, and starts downloading them into a local location. Once the download is complete, it will delete the files on the FTP server. In my next blog, I will show how to do JUnit testing of Spring Integration flow with SFTP Server.

Spring Integration Flow

Spring Integration FakeFtpServer example

Spring Integration FakeFtpServer example

In order to use FakeFtpServer, we need to have Maven dependency as below:

XML
<dependency>
<groupId>org.mockftpserver</groupId>
<artifactId>MockFtpServer</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>

The first step to this is to create a FakeFtpServer before every test runs as below:

Java
@Before
public void setUp() throws Exception {
fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setServerControlPort(9999); // use any free port
FileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new FileEntry(FILE, CONTENTS));
fakeFtpServer.setFileSystem(fileSystem);
UserAccount userAccount = new UserAccount("user", "password", HOME_DIR);
fakeFtpServer.addUserAccount(userAccount);
fakeFtpServer.start();
}

@After
public void tearDown() throws Exception {
fakeFtpServer.stop();
}

Finally, run the JUnit test case as below:

Java
@Autowired
private FileDownloadUtil downloadUtil;
@Test
public void testFtpDownload() throws Exception {
File file = new File("src/test/resources/output");
delete(file);
FTPClient client = new FTPClient();
client.connect("localhost", 9999);
client.login("user", "password");
String files[] = client.listNames("/dir");
client.help();
logger.debug("Before delete" + files[0]);
assertEquals(1, files.length);
downloadUtil.downloadFilesFromRemoteDirectory();
logger.debug("After delete");
files = client.listNames("/dir");
client.help();
assertEquals(0, files.length);
assertEquals(1, file.list().length);
}

I hope this blog helped.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)