Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Spring integration FakeFtpServer example

, 24 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this blog, we will test a Spring Integration flow which checks for a list of files, apply a splitter to separate each file, and start downloading them into a local location.

For people in 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 a interesting topic, there are a few articles like Unit testing file transfers, which gives some insight on this topic.

In this blog, we will test a Spring Integration flow which checks for a list of files, apply a splitter to separate each file, and start 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,

<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,

@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,

@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)

Share

About the Author

skprasadu

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 24 Dec 2012
Article Copyright 2012 by skprasadu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid