【问题标题】:How can I mock this piece of code of Spring Boot using Mockito?如何使用 Mockito 模拟这段 Spring Boot 代码?
【发布时间】:2022-01-13 10:20:06
【问题描述】:

考虑:

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

我已经使用 Mockito 模拟了附件列表,所以我得到了附件列表。但是我应该如何模拟剩下的代码呢?我什至嘲笑过过滤器权限。

【问题讨论】:

  • 1) 你想测试什么? 2)也许只是使用预定义的列表而不是模拟它?
  • 我正在测试我的控制器,所以我在我的控制器中注入了这个服务,上面的代码是我的服务层的。
  • 您到底想测试什么:该服务的方法是用正确的参数调用的,还是您正在编写集成测试?
  • (Stack Overflow 上的语法高亮显示:为什么变量“attachmentBO”在声明中的颜色与其使用位置不同?为什么(数组)变量“@”不同987654324@" in this question?)

标签: java spring-boot mockito


【解决方案1】:

不要模拟列表元素。取而代之的是模拟更高的级别以返回一个测试列表

澄清一下:

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

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

然后在你的业务逻辑中:

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

业务逻辑将返回您在测试中创建的List.of(attachments),并运行所有这些,包括map/collect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2022-12-11
    • 2021-05-28
    相关资源
    最近更新 更多