如何解决如何为 ZipInputStream.getNextEntry 编写 Junit
@Test
public void validateExcelTestWithImagesFolder() throws IOException {
final MediaModel excelmediaModel = Mockito.mock(MediaModel.class);
final Workbook workbook = Mockito.mock(Workbook.class);
final Impex impex = Mockito.mock(Impex.class);
final File image = Mockito.mock(File.class);
final ZipEntry entry = Mockito.mock(ZipEntry.class);
final InputStream inputStream = Mockito.mock(InputStream.class);
final ZipInputStream zip = Mockito.mock(ZipInputStream.class);
when(cronJobModel.getReferencedContent()).thenReturn(media);
when(cronJobModel.getExcelFile()).thenReturn(media);
when(cronJobModel.getExcelFile()).thenReturn(media);
when(userService.getCurrentUser()).thenReturn(user);
when(userService.getCurrentUser().getUid()).thenReturn(UID);
when(image.isFile()).thenReturn(Boolean.TRUE);
when(image.getParentFile()).thenReturn(image);
when(image.getParentFile().getName()).thenReturn(PARENTFILENAME);
when(image.length()).thenReturn(10000000l);
when(image.getName()).thenReturn(IMAGEFILENAME1);
when(image.isDirectory()).thenReturn(true);
when(entry.isDirectory()).thenReturn(true);
when(entry.getName()).thenReturn(IMAGEFILENAME1);
when(zip.getNextEntry()).thenReturn(entry);
when(cronJobModel.getExcelFile()).thenReturn(excelmediaModel);
when(excelWorkbookService.createWorkbook(this.mediaService.getStreamFromMedia(cronJobModel.getExcelFile()))).thenReturn(workbook);
when(productExcelImportService.convertToImpex(workbook,Boolean.TRUE)).thenReturn(impex);
when(mediaService.getStreamFromMedia(cronJobModel.getReferencedContent())).thenReturn(inputStream);
List<String> validationResult = ExcelImportValidator.generateFolderListForZip(cronJobModel);
Assert.assertNotNull(validationResult);
}
以下是实际的类方法
private List<String> generateFolderListForZip(ExcelImportCronJobModel cronJob) {
final List<String> foldersInZip = new ArrayList<>();
try {
if(cronJob.getReferencedContent() !=null) {
final ZipInputStream zip = new ZipInputStream(this.mediaService.getStreamFromMedia(cronJob.getReferencedContent()));
ZipEntry entry = null;
while ((entry = zip.getNextEntry()) != null) {
if(entry.isDirectory()) {
foldersInZip.add(entry.getName().split(CoreConstants.FORWARD_SLASH)[0]);
}
}
}
} catch (IOException e) {
LOG.error("Error reading zip,e");
}
return foldersInZip;
}
问题:
当我尝试在本地调试测试方法时,控件转到这一行 while ((entry = zip.getNextEntry()) != null) {
并且它卡住了并且在 than 之后没有发生任何事情。意味着 intellij 什么都不做,一段时间后它退出方法“进程以退出代码 137 结束(被信号 9:SIGKILL 中断)”。
当我尝试进行评估时,窗口一直显示“正在评估”,但没有任何反应。我确定,我做错了什么并且不确定我在测试方法中设置模拟的方式是非常正确,如果您能指导我,那就太好了。
我正在编写的单元测试只是@UnitTest 而不是集成测试。如果您需要更多信息,请与我联系。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。