0

Consider:

return attachmentList.stream().map(attachment -> {
    AttachmentBO attachmentBO = new AttachmentBO();
    attachmentBO.setId(attachment.getId());
    attachmentBO.setTitle(attachment.getName());
    attachmentBO.setType(attachment.getValue().get("type").toString());
    attachmentBO.setCreatorId(attachment.getAuditUserId());
    String[] filteredPermissionsForNote = this.filterPermissionCommand.filterPermissions(answer.getTopicId(), attachment.getAuditUserId(), topicDetails.getPermissions(), topicDetails.getEffectiveRoles(), true);
    attachmentBO.setPermissions(filteredPermissionsForNote);

    if (attachmentBO.getType().equalsIgnoreCase("URL")) {
        attachmentBO.setUrl(String.valueOf(attachment.getMediaInfo().get("url")));
    } else if (attachmentBO.getType().equalsIgnoreCase("FILE")) {
        attachmentBO.setSize((Double) attachment.getValue().get("size"));
    }

    return attachmentBO;
}).collect(Collectors.toList());

I have mocked the attachmentList using Mockito, so I am getting the attachmentList. But how should I mock the remaining code? I have even mocked filterpermission.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    1) What are you trying to test? 2) Maybe just use predefined list instead of mocking it? – Barracuda Jan 13 '22 at 10:25
  • I am testing my controller so I am injecting this service in my controller the above code is of my service layer. – Zahid M Shaikh Jan 13 '22 at 11:17
  • 1
    And what exactly you want to test: that service's method is called with proper argument or are you writing an integration test? – Barracuda Jan 13 '22 at 11:19
  • (Syntax highlighting here on Stack Overflow: Why does variable "`attachmentBO`" have a different colour in the declaration compared to where it is used? Why is it different for the (array) variable "`ret`" in [this question](https://stackoverflow.com/questions/960431/)?) – Peter Mortensen Mar 09 '22 at 12:32

1 Answers1

0

Do not mock the list elements. Instead mock one level higher to return you a test list instead.

To clarify:

List<Attachment> attachmentList = List.of(RealAttachment1, RealAttachment2); // <-- Nothing is using Mockito here.

when(someMethodWhichReturnsAttachmentList()).thenReturn(attachmentList);

Then in your business logic:

attachmentList = someMethodWhichReturnsAttachmentList(); // Mockito will return you the test list you created earlier.


// You will now map the test List elements, as in a normal business logic
return attachmentList.stream().map(attachment -> {
    AttachmentBO attachmentBO = new AttachmentBO();
    attachmentBO.setId(attachment.getId());
    attachmentBO.setTitle(attachment.getName());
    attachmentBO.setType(attachment.getValue().get("type").toString());
    attachmentBO.setCreatorId(attachment.getAuditUserId());
    String[] filteredPermissionsForNote = this.filterPermissionCommand.filterPermissions(answer.getTopicId(), attachment.getAuditUserId(), topicDetails.getPermissions(), topicDetails.getEffectiveRoles(), true);
    attachmentBO.setPermissions(filteredPermissionsForNote);

    if (attachmentBO.getType().equalsIgnoreCase("URL")) {
        attachmentBO.setUrl(String.valueOf(attachment.getMediaInfo().get("url")));
    } else if (attachmentBO.getType().equalsIgnoreCase("FILE")) {
        attachmentBO.setSize((Double) attachment.getValue().get("size"));
    }

    return attachmentBO;
}).collect(Collectors.toList());

The business logic will return the List.of(attachments) you created in your test, and run through them all, including map/collect.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JCompetence
  • 6,997
  • 3
  • 19
  • 26