I need to test Apache camel SFTP consumer. Is there a way to run an embedded SFTP server to test my code in dev environment? Appreciate any help on this.
Asked
Active
Viewed 4,419 times
1
-
http://stackoverflow.com/questions/3076443/java-sftp-server-library?rq=1 comes with a full Apache Mina SSHD example – Redfoxmoon Nov 19 '16 at 17:44
-
I'd second Mina to be the best answer for you. I don't know of another way to run an embedded server. Another option would be to look for a mock FTP server, such as the one detialed here: https://itsiastic.wordpress.com/2012/11/08/how-to-create-a-java-ftp-server-mock/ – Michael Hoffman Nov 20 '16 at 05:55
-
Thanks Guys! Apache Mina looks promising at least for the testing I need to perform. Can you also point me to correct direction for how I can generate keys for authentication on this embedded server? – Techji Nov 20 '16 at 14:32
1 Answers
2
Once I had a similiar problem and therefore wrote a JUnit 4 rule that runs an SFTP server during your test. It is called Fake SFTP Server Rule and uses Apache Mina SSHD internally. Here is an example of a test using that rule:
public class SomeTest {
@Rule
public final FakeSftpServerRule sftpServer = new FakeSftpServerRule();
@Test
public void testTextFile() {
//code that uploads the file
String fileContent = sftpServer.getFileContent("/directory/file.txt", UTF_8);
//verify content
}
}

Stefan Birkner
- 24,059
- 12
- 57
- 72
-
-
1@Stefan Birken, the example of the code above produce: java.lang.IllegalStateException: Failed to download file because test has not been started or is already finished (Junit 4.12 + java 11) – Alex85 Jun 01 '21 at 13:20
-
I don't want to do inside @Test method, I wanted to do in automation testing server I need to make it up, how I can achieve that? – codeSeeker Dec 17 '21 at 12:51
-
I worked with your fake sftp server a lot, thank you! now I have to add support to RSA key, do you support? – AsfK Feb 07 '22 at 10:37