I am updating a few tests. The Before gets the sftp session. For this, the username and password have been hardcoded in the properties file. Due to security reasons, the password cannot be checked in and has to be blanked out. However the unit test fails at
private DefaultSftpSessionFactory sftpClientFactory;
private SftpSession sftpSession;
@Before
public void setup() {
sftpSession = sftpClientFactory.getSession();
}
This step fails with "either a password or private key is required".I would like to get a mock session, so that I dont have to provide a password.
In case you are using JUnit 5 Jupiter (might be easily adaptable to JUnit 4 as well) I´ve written an article on how to do it using the atmoz/sftp Docker image together with Testcontainers.
The full working example can be found here
https://overflowed.dev/blog/sftp-testing-junit-testcontainers-atmoz-sftp/
This is basically how you would define your TestContainer with SFTP
private static final GenericContainer sftp = new GenericContainer(
new ImageFromDockerfile()
.withDockerfileFromBuilder(builder ->
builder
.from("atmoz/sftp:latest")
.run("mkdir -p /home/" + USER + "/upload; chmod -R 007 /home/" + USER)
.build()))
//.withFileSystemBind(sftpHomeDirectory.getAbsolutePath(), "/home/" + USER + REMOTE_PATH, BindMode.READ_WRITE) //uncomment to mount host directory - not required / recommended
.withExposedPorts(PORT)
.withCommand(USER + ":" + PASSWORD + ":1001:::upload");
Regarding the unanswered/able question What's the best mock framework for Java?, a Mockito approach would be:
Get the needed libraries into your (test) class path. (https://mvnrepository.com/artifact/org.mockito)
Mock the SftpSession. (Via annotation private @Mock SftpSession sftpSession; ...plus the according initialization/enablement, or (manually) via sftpSession = Mockito.mock(SftpSession.class);)
a. See, whether the SessionFactory is needed (for test) at all, if so also mock.
Mock/verify/reset any interactions (within your tests) with the mocked objects. (Like Mockito.when(sftpSession.foo(x,y,z)).then... or Mockito.verify(sftpSession, Mockito.times(n)).foo(x,y,z);)
Further reading:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With