Enterprise Integration Zone is brought to you in partnership with:

Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

Using Spring FakeFtpServer to JUnit test a Spring Integration Flow

12.13.2012
| 3957 views |
  • submit to reddit
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 an interesting topic, and there are few articles on 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 seen 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.



Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)