简体   繁体   中英

How to mock this piece of code of spring boot using Mockito?

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.

Do not mock the List elements, instead mock 1 level higher to return you a Test List instead.

To clarify:

List<Attachment> attachmentList = List.of(RealAttachment1, RealAttachment2); <--Nothing 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM