首页 > 解决方案 > 模型映射器模拟在 Spring Boot 单元测试中返回空对象

问题描述

我正在尝试使用 MockMvc 和 Mockito 在我的应用程序中为休息控制器类编写单元测试。我的实体类有 DTO 类,我将其作为控制器方法的输入。控制器方法将此 DTO 对象映射到实体类并使用我的服务类将其持久化。持久化后,通过映射服务类方法返回的对象创建一个新的 DTO 类,并在 ResponseEntity 对象中返回该 DTO。在我的单元测试中,我使用@MockBean 注解模拟了服务类和 ModelMapper 类。我还在为模拟类的方法设置预期的返回值。但是当我运行测试时,我看到响应体是空的,我认为这是因为模拟映射器没有正确返回 DTO 对象。有人可以帮我让模拟映射器正确返回对象,以便我的测试通过吗?谢谢。

这是控制器代码:

@RequestMapping(value = "", method=RequestMethod.POST)
    public ResponseEntity<BranchDto> addBranch(@RequestBody BranchDto branchDto) {
        Branch branch = modelMapper.map(branchDto, Branch.class);
        Branch addedBranch = branchService.addBranch(branch);
        return new ResponseEntity<>(modelMapper.map(addedBranch, BranchDto.class), HttpStatus.CREATED);
    }

这是单元测试代码:

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private BranchService branchService;

    @MockBean
    private ModelMapper mockModelMapper;

    @Test
    public void testAddBranch() throws Exception{
        BranchDto mockBranchDtoToAdd = new BranchDto();
        mockBranchDtoToAdd.setBranchName("TestBranch");
        mockBranchDtoToAdd.setContactNumber("12345");
        mockBranchDtoToAdd.setEmailId("test@abc.com");
        mockBranchDtoToAdd.setCity("TestCity");
        Branch mockBranchToAdd = new Branch();
        mockBranchToAdd.setBranchName("TestBranch");
        mockBranchToAdd.setContactNumber("12345");
        mockBranchToAdd.setEmailId("test@abc.com");
        mockBranchToAdd.setCity("TestCity");

        Branch mockAddedBranch = new Branch();
        mockAddedBranch.setBranchName("TestBranch");
        BranchDto mockAddedBranchDto = new BranchDto();
        mockAddedBranchDto.setBranchName("TestBranch");
        mockAddedBranchDto.setContactNumber("12345");
        mockAddedBranchDto.setEmailId("test@abc.com");
        mockAddedBranchDto.setCity("TestCity");

        Mockito.when(mockModelMapper.map(mockBranchDtoToAdd, Branch.class)).thenReturn(mockBranchToAdd);
        Mockito.when(branchService.addBranch(mockBranchToAdd)).thenReturn(mockAddedBranch);
        Mockito.when(mockModelMapper.map(mockAddedBranch, BranchDto.class)).thenReturn(mockAddedBranchDto);


        ObjectMapper mapper = new ObjectMapper();
        String mockBranchDtoToAddStr = mapper.writeValueAsString(mockBranchDtoToAdd);
        System.out.println(mockBranchDtoToAddStr);
        mockMvc.perform(post("/branches").contentType(MediaType.APPLICATION_JSON).content(mockBranchDtoToAddStr))
        .andExpect(MockMvcResultMatchers.status().isCreated())
        .andExpect(MockMvcResultMatchers.jsonPath("$.branchName").value("TestBranch"));
    }

标签: javaspring-bootunit-testingmockito

解决方案


经过大量挖掘,我发现这条线

Mockito.when(branchService.addBranch(mockBranchToAdd)).thenReturn(mockAddedBranch);

没有正确设置模拟对象。我将此行更改为any()在该when()方法中使用,此后它运行良好。这是更新的代码:

Mockito.when(branchService.addBranch(org.mockito.ArgumentMatchers.any())).thenReturn(mockAddedBranch);
Mockito.when(mockModelMapper.map(org.mockito.ArgumentMatchers.any(), org.mockito.ArgumentMatchers.any())).thenReturn(mockAddedBranch);

推荐阅读