1

I have found a mock SFTP server implementation to be used as @Rule injection within JUnit tests. This lets you install a mock SFTP server. For that simply a dependency has to be added to the projects pom.xml with test scope:

        <dependency>
            <groupId>com.github.stefanbirkner</groupId>
            <artifactId>fake-sftp-server-rule</artifactId>
            <version>2.0.1</version>
            <scope>test</scope>
        </dependency>

My integration flow so far is (all the parameters are injected with the @ConfigurationProperties annotation):

@Bean
public SessionFactory<LsEntry> sftpTest1SessionFactory() {
    DefaultSftpSessionFactory sf = new DefaultSftpSessionFactory();
    sf.setHost(hostname);
    sf.setPort(port);
    sf.setUser(username);
    sf.setPassword(password);
    return new CachingSessionFactory<LsEntry>(sf);
}

@Bean
public FireOnceTrigger fireOnceTrigger() {
    return new FireOnceTrigger();
}

@Bean
public IntegrationFlow test1SftpInboundFlow() {
    return IntegrationFlows
        .from(Sftp.inboundAdapter(sftpTest1SessionFactory)
                .preserveTimestamp(true)
                .remoteDirectory(remoteDir)
                .regexFilter(remoteFilePattern)
                .localFilenameExpression(localFile)
                .localDirectory(new File(localDir)),
             e -> e.id("sftpTest1InboundAdapter")
                .autoStartup(true)
                .poller(Pollers.trigger(fireOnceTrigger()))
             )
        .transform(e -> e)
        .handle(m -> System.out.println(m.getPayload()))
        .get();
}

Is it possible to combine my integration flow in a test case with this mocked SFTP server? How would I do this?

JBStonehenge
  • 192
  • 3
  • 15

1 Answers1

1

Checkout the spring integration unit tests. The ftp unit tests are at...

https://github.com/spring-projects/spring-integration/tree/main/spring-integration-ftp/src/test/java/org/springframework/integration/ftp.

FtpTestSupport shows how they setup an embedded FTP server used by the tests...

https://github.com/spring-projects/spring-integration/blob/main/spring-integration-ftp/src/test/java/org/springframework/integration/ftp/FtpTestSupport.java

httPants
  • 1,832
  • 1
  • 11
  • 13
  • Yes, we use Apache Mina project to add fake (s)ftp servers to our tests. In most cases it is really about test class level rules and using those static properties in the Spring configuration as a reference from session factory for that fake host/port. – Artem Bilan Aug 16 '21 at 03:43
  • Thank you! Do I have to copy those TestSupport classes or are they part of any package org.springframework.integration I could load into my project via maven dependency? – JBStonehenge Aug 22 '21 at 22:45
  • Which maven dependency versions do I need then for apache-sshd and apache-ftp-server? – JBStonehenge Aug 23 '21 at 00:43