0

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

user207421
  • 305,947
  • 44
  • 307
  • 483
Karthik
  • 371
  • 3
  • 7
  • 30
  • It's because you never return null from `getNextEntry()`. You always return the same `entry`. What is the purpose of this test? – user207421 May 12 '21 at 03:19
  • The purpose of this test is that, when a customer upload a zip files which contains the collection of images, all the images should reside inside a folder which is inside the zip.None of the image should be outside the folder.So we are validating those images and adding to the list. – Karthik May 12 '21 at 04:43
  • Well your method doesn't enforce that, and nor does your test test it. All you are really testing is whether your method returns non-null, which it always does. If there are no folders it returns an empty list. Nowhere does anybody test for the presence or location of image files. – user207421 May 12 '21 at 06:39
  • hi @user207421 thanks for your comments.unfortunately i havent added all my code code over here ,its just that I wanted inputs to overcome the issue where my test case stuck at this line " while ((entry = zip.getNextEntry()) != null) { " .However I tried to over this mmethod in the test class by having a inner class inside the test class by extending the main class which have this above method ,the entire while loop keeps iterating infinite times as i have set up the mock for while ((entry = zip.getNextEntry()) != null) { .How can i fix that infinite loop issue. – Karthik May 13 '21 at 06:13
  • By returning `null` from `getNextEntry()` at some point, instead of always returning `entry`, as I already stated, yesterday. Surely this is obvious? – user207421 May 13 '21 at 10:17
  • Hi @user207421 In order to do the code coverage inside the while loop,I did two things ,1.Did a mock object setup - by doing this the control didn't went inside the while loop,2.I override the method in the test class by having a inner class which extends the main class something like this private class ExcelImportValidatorInner extends ExcelImportValidator { //override method -> protected ZipInputStream getZipInputStream(InputStream streamFromMedia) { return zip; -> is a Mock object. } } Question is how to return null for getNextEntry() ? – Karthik May 13 '21 at 23:26
  • You need to adjust your `thenReturn()` for it so it returns `entry` the first time and `null` thereafter. Surely this is obvious? – user207421 May 14 '21 at 03:31

0 Answers0