How to write a Junit for ZipInputStream.getNextEntry

1k Views Asked by At

I have the following junit test code method inside a junit class

    @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);

}

Following is the actual class method

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;
}

Issue :

When I am trying to debug the test method in local,the control goes to this line while ((entry = zip.getNextEntry()) != null) { and its getting stuck and nothings happening after than.Means the intellij does nothing and after a while it exits the method with "Process finished with exit code 137 (interrupted by signal 9: SIGKILL)".

When I tried to do evaluation, I am getting the window keep showing "Evaluating" but nothing happens.I am sure,I am doing something wrong and am not sure that way that I have set the mock in the test method is quite right, would be great if you could guide me on this please.

The unit test that I am writing is just @UnitTest and its not an integration test.Please reach out to me if you need more information.

enter image description here

0

There are 0 best solutions below