我正在尝试使用 junit 测试 SftpSession.read 方法,我不太确定测试此方法的方法是什么,我正在使用 @Captor,但我不清楚我可以实现吗这个测试。这是我的测试代码:
@Test
void getFileContent() throws IOException {
String directory = "testDirectory";
String fileName = "testFileName";
ByteArrayOutputStream fileByteStream = new ByteArrayOutputStream();
given(sessionFactory.getSession()).willReturn(sftpSession);
sftpServiceImpl.getFileContent(directory, fileName);
verify(sftpSession, times(1)).read(captor.capture(), eq(fileByteStream));
}
这是 getFileContent 方法:
@Override
public byte[] getFileContent(String directory, String fileName) throws IOException {
try (SftpSession session = sessionFactory.getSession()) {
ByteArrayOutputStream fileByteStream = new ByteArrayOutputStream();
session.read(directory + "/" + fileName, fileByteStream);
return fileByteStream.toByteArray();
}
}
最佳答案
你可以这样做:
verify(sftpSession, times(1)).read(any(String.class), any(ByteArrayOutputStream.class));
或
verify(sftpSession, times(1)).read(argThat (arg -> arg.contains(directory) && arg.contains(fileName) ), argThat (arg -> arg != null));
关于java - 使用 @Capture Mockito 进行测试时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59777445/