简体   繁体   English

model 映射器模拟在 Z2A2D595E6ED9A0B24F027F2B63B134D 引导单元中返回 null object

[英]model mapper mock returns null object in spring boot unit test

I am trying to write unit tests for a rest controller class in my application using MockMvc and Mockito.我正在尝试使用 MockMvc 和 Z4D13B33CA816B79B3E 在我的应用程序中为 rest controller class 编写单元测试I have DTO class for my entity class which I give as input for the controller method.我的实体 class 有 DTO class,我将其作为 controller 方法的输入。 The controller method maps this DTO object into the entity class and persists it using my service class. controller 方法将此 DTO object 映射到实体 class 并使用我的服务 ZA2F2ED4F8EBC40AB4C21A29 保留它After persisting, a new DTO class is created by mapping the object returned by the service class' method and this DTO is returned in the ResponseEntity object.持久化后,通过映射服务类方法返回的 object 创建一个新的 DTO class,并在 ResponseEntity object 中返回此 DTO。 In my unit test, I have mocked the service class and the ModelMapper class using @MockBean annotation.在我的单元测试中,我使用 @MockBean 注释模拟了服务 class 和 ModelMapper class 。 I am also setting the expected return values for the methods of the mocked class.我还为模拟的 class 的方法设置了预期的返回值。 But when I run the test, I see that the response body is empty, which I assume is because the mock mapper is not returning the DTO object correctly.但是当我运行测试时,我看到响应体是空的,我认为这是因为模拟映射器没有正确返回 DTO object。 Can someone please help me to make the mock mapper return the object correctly so that my tests pass?有人可以帮我让模拟映射器正确返回 object 以便我的测试通过吗? Thanks.谢谢。

Here's the controller code:这是 controller 代码:

@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);
    }

Here's the unit test code:这是单元测试代码:

    @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"));
    }

After a lot of digging, I figured that the line经过大量挖掘,我发现这条线

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

was not setting the mock object correctly.没有正确设置模拟 object。 I changed this line to use any() in the when() method and it worked fine thereafter.我将此行更改为在when()方法中使用any() ,此后它运行良好。 Here's the updated code:这是更新的代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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