如何从模拟中 return SftpFileInfo? Junit5 Java11 spring-集成
How to return SftpFileInfo from a mock? Junit5 Java11 spring-integration
在我的配置中
@MessagingGateway
public interface SftpMessagingGateway {
@Gateway(requestChannel = "listFiles")
List<SftpFileInfo> readListOfFiles(String payload);
}
@Bean
@ServiceActivator(inputChannel = "listFiles")
public MessageHandler sftpReadHandler(){
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "ls", remoteDirectory);
return sftpOutboundGateway;
}
在我的代码中,我称之为:
List<SftpFileInfo> remoteFiles = sftpGateway.readListOfFiles("payload")
并使用 SftpFileInfo 中的文件名来执行一些操作。在我的测试中,我想模拟网关和 return 特定的文件名;
我在测试中有这个:
ChannelSftp.LsEntry lsEntry = new ChannelSftp().new LsEntry("filename", "filename", null);
Mockito.when(sftpMessagingGateway.readListOfFiles(Mockito.anyString())).thenReturn(List.of(new SftpFileInfo(lsEntry)));
我需要使模拟 return 成为一个带有文件名的 LsEntry 的 SftpFileInfo,以便能够正确地 运行 通过我的代码,我查看了 class 并查看:
LsEntry(String filename, String longname, SftpATTRS attrs){
setFilename(filename);
setLongname(longname);
setAttrs(attrs);
}
有没有办法让我在测试中做到这一点?目前,我无法设置 LsEntry,因为它无法在包外访问
回答后:
ChannelSftp.LsEntry lsEntry = mock(ChannelSftp.LsEntry.class);
SftpATTRS attributes = mock(SftpATTRS.class);
when(lsEntry.getAttrs()).thenReturn(attributes);
when(lsEntry.getFilename()).thenReturn("file");
SftpFileInfo fileInfo = new SftpFileInfo(lsEntry);
List<SftpFileInfo> sftpFileInfoList = List.of(fileInfo);
Mockito.when(sftpMessagingGateway.readListOfFiles(Mockito.anyString())).thenReturn(sftpFileInfoList);
我们在 Spring 集成测试中这样做:
SftpATTRS attrs = mock(SftpATTRS.class);
Constructor<LsEntry> ctor = (Constructor<LsEntry>) LsEntry.class.getDeclaredConstructors()[0];
ctor.setAccessible(true);
LsEntry sftpFile1 = ctor.newInstance(channel, "foo", "foo", attrs);
我认为类似的东西也应该适用于您的模拟。
另一种方法是直接模拟它:
LsEntry lsEntry = mock(LsEntry.class);
SftpATTRS attributes = mock(SftpATTRS.class);
when(lsEntry.getAttrs()).thenReturn(attributes);
when(lsEntry.getFilename()).thenReturn(fileName);
在我的配置中
@MessagingGateway
public interface SftpMessagingGateway {
@Gateway(requestChannel = "listFiles")
List<SftpFileInfo> readListOfFiles(String payload);
}
@Bean
@ServiceActivator(inputChannel = "listFiles")
public MessageHandler sftpReadHandler(){
SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(sftpSessionFactory(), "ls", remoteDirectory);
return sftpOutboundGateway;
}
在我的代码中,我称之为:
List<SftpFileInfo> remoteFiles = sftpGateway.readListOfFiles("payload")
并使用 SftpFileInfo 中的文件名来执行一些操作。在我的测试中,我想模拟网关和 return 特定的文件名;
我在测试中有这个:
ChannelSftp.LsEntry lsEntry = new ChannelSftp().new LsEntry("filename", "filename", null);
Mockito.when(sftpMessagingGateway.readListOfFiles(Mockito.anyString())).thenReturn(List.of(new SftpFileInfo(lsEntry)));
我需要使模拟 return 成为一个带有文件名的 LsEntry 的 SftpFileInfo,以便能够正确地 运行 通过我的代码,我查看了 class 并查看:
LsEntry(String filename, String longname, SftpATTRS attrs){
setFilename(filename);
setLongname(longname);
setAttrs(attrs);
}
有没有办法让我在测试中做到这一点?目前,我无法设置 LsEntry,因为它无法在包外访问
回答后:
ChannelSftp.LsEntry lsEntry = mock(ChannelSftp.LsEntry.class);
SftpATTRS attributes = mock(SftpATTRS.class);
when(lsEntry.getAttrs()).thenReturn(attributes);
when(lsEntry.getFilename()).thenReturn("file");
SftpFileInfo fileInfo = new SftpFileInfo(lsEntry);
List<SftpFileInfo> sftpFileInfoList = List.of(fileInfo);
Mockito.when(sftpMessagingGateway.readListOfFiles(Mockito.anyString())).thenReturn(sftpFileInfoList);
我们在 Spring 集成测试中这样做:
SftpATTRS attrs = mock(SftpATTRS.class);
Constructor<LsEntry> ctor = (Constructor<LsEntry>) LsEntry.class.getDeclaredConstructors()[0];
ctor.setAccessible(true);
LsEntry sftpFile1 = ctor.newInstance(channel, "foo", "foo", attrs);
我认为类似的东西也应该适用于您的模拟。
另一种方法是直接模拟它:
LsEntry lsEntry = mock(LsEntry.class);
SftpATTRS attributes = mock(SftpATTRS.class);
when(lsEntry.getAttrs()).thenReturn(attributes);
when(lsEntry.getFilename()).thenReturn(fileName);