Skip to content

JUnit 5 extension that allows you to write unit/integration tests against a in memory SFTP server.

License

Notifications You must be signed in to change notification settings

ricall/junit5-sftp

Repository files navigation

JUnit 5 SFTP server

What is it?

This JUnit 5 libary provides a way to create a sftp server that can be used for integration testing.

How to use it

Gradle

Add the dependency to gradle

testImplementation 'io.github.ricall.junit5-sftp:junit5-sftp:2.0.1'

Maven

Add the dependency to mvn pom.xml

<dependency>
    <groupId>io.github.ricall.junit5-sftp</groupId>
    <artifactId>junit5-sftp</artifactId>
    <version>2.0.1</version>
    <scope>test</scope>
</dependency>

Using the JUnit5 extension

public class TestEmbeddedSftpServer {

    @RegisterExtension
    public final EmbeddedSftpServer sftpServer = SftpServer.defaultSftpServer()
            .withPort(3022)
            .withUser("user", "pass")
            .withResources(resourceAt("/tmp/data").fromClasspathResource("/data"))
            .build();

    @Test
    public void verifySftpFilesCanBeDownloaded() {
        // sftp server is running on sftp://user:pass@localhost:3022
        // the files and directories from the data package are copied into the /tmp/data folder
        // on the sftp server
    }
    
}

The FTP server uses an in-memory FileSystem to manage files, between tests the file system is recreated so that tests do not impact each other.

Using the FileSystemResource abstraction.

@SftpEmbeddableServerExtension provides a powerful FileSystemResource abstraction that allows you to populate the in-memory FileSystem with files/folders in a simple easy to use fashion.


FileSystemResource withText

Return a FileSystemResource that will populate the /tmp/file1.txt file with File Contents

FileSystemResource.resourceAt("/tmp/file1.txt").withText("File Contents")

FileSystemResource withContent

Return a FileSystemResource that will populate the /tmp/file2.txt file with the data returned from createFileInputStream()

FileSystemResource.resourceAt("/tmp/file2.txt").withContent(this::createFileInputStream)

FileSystemResource fromClasspathResource

Return a list of FileSystemResource's that will populate the /tmp/test-data folder with all files/directories under the test.data package.

FileSystemResource.resourceAt("/tmp/test-data").fromClasspathResource("/test/data")

FileSystemResource fromPath

Return a list of FileSystemResource's that will populate the /tmp/folder folder with all the files/directories under path (path can be files/folders on your hard drive, files/folders in a ZIP/TAR container or event files/folders in another in-memory filesystem)

FileSystemResource.resourceAt("/tmp/folder").fromPath(path)

License

This software is licensed using MIT

About

JUnit 5 extension that allows you to write unit/integration tests against a in memory SFTP server.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages