Over a million developers have joined DZone.

Spring Integration Mock SftpServer Example

DZone's Guide to

Spring Integration Mock SftpServer Example

In this example I will show how to test Spring Integration flow using Mock SftpServer.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

In continuation of my earlier blog Spring Integration FakeFtpServer example in this example I will show how to test Spring Integration flow using Mock SftpServer. There a are few good writeups on the net including the Stackoverflow writeup, Using Apache Mina as a Mock/In Memory SFTP Server for Unit Testing. The code for this blog is @ Spring Integration flow to test Ftp/Sftp server.

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

Again talking about the same spring integration flows as mentioned in my earlier blog, I will write a test for a sftp server,

Spring Integration Mock SftpServer example

Spring Integration Mock SftpServer example

The maven dependency for this is as below,


The startup and teardown of the junit is as below,

public void beforeTestSetup() throws Exception {
sshd = SshServer.setUpDefaultServer();

sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));
sshd.setPasswordAuthenticator(new PasswordAuthenticator() {

public boolean authenticate(String username, String password, ServerSession session) {
// TODO Auto-generated method stub
return true;

CommandFactory myCommandFactory = new CommandFactory() {

public Command createCommand(String command) {
System.out.println("Command: " + command);
return null;
sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));

List<NamedFactory<command>> namedFactoryList = new ArrayList<NamedFactory<command>>(); namedFactoryList.add(new SftpSubsystem.Factory()); sshd.setSubsystemFactories(namedFactoryList); sshd.start(); }


public void teardown() throws Exception { sshd.stop(); }

The Junit test is as below,

public void testPutAndGetFile() throws Exception {
JSch jsch = new JSch();

Hashtable config = new Hashtable();
config.put("StrictHostKeyChecking", "no");

Session session = jsch.getSession("remote-username", "localhost", 22999);


Channel channel = session.openChannel("sftp");

ChannelSftp sftpChannel = (ChannelSftp) channel;

final String testFileContents = "some file contents";

String uploadedFileName = "uploadFile";
sftpChannel.put(new ByteArrayInputStream(testFileContents.getBytes()), uploadedFileName);

String downloadedFileName = "downLoadFile";
sftpChannel.get(uploadedFileName, downloadedFileName);

File downloadedFile = new File(downloadedFileName);

String fileData = getFileContents(downloadedFile);

assertEquals(testFileContents, fileData);

if (sftpChannel.isConnected()) {
logger.debug("Disconnected channel");

if (session.isConnected()) {
logger.debug("Disconnected session");


I hope this blog helped you.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

java ,enterprise-integration ,tutorial ,integration ,spring integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}