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

Pingback: Spring Integration Mock SftpServer example | GoSmarter Tech Blog
Nice tutorial krishna, this solves the problem of unit testing ftp /sftp projects
nice tutorial krishna