何时/ thenReturn未映射

时间:2018-05-10 11:52:54

标签: junit mockmvc springmockito

我无法理解为什么我的/ thenReturn没有映射到模拟bean。

在调试期间,我可以看到“userDTO userDto = userService.update(id,entity);”之后的“userDto”;在测试的控制器中等于null。

知道我做错了吗?

测试文件UsersControllerTest.java:

    package com.mycomp.mymessagesys.controllers;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycomp.mymessagesys.controller.UsersController;
import com.mycomp.mymessagesys.model.UserDTO;
import com.mycomp.mymessagesys.service.UserService;

@RunWith(SpringRunner.class)
@WebMvcTest(UsersController.class)
public class UsersControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper jacksonMapper;

    @MockBean
    private UserService userService;

    @MockBean
    private UserDTO userDto;


    private UserDTO createUser(String id, int age, String name) {
        UserDTO userEntity = new UserDTO();
        userEntity.setId(id);
        userEntity.setAge(age);
        userEntity.setName(name);
        return userEntity;
    }

    @Test
    public void testUpdate() throws Exception {
        UserDTO userEntity = createUser("666", 66, "User_6");
        when(userService.update("666", userEntity)).thenReturn(userEntity);
        this.mockMvc.perform(put(UsersController.URL + "/666").contentType(MediaType.APPLICATION_JSON)
                .content(jacksonMapper.writeValueAsString(userEntity)))
        .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
        .andExpect(status().isOk());
    }


}

经过测试的UsersController类:

package com.mycomp.mymessagesys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.mycomp.mymessagesys.model.UserDTO;
import com.mycomp.mymessagesys.service.UserService;

@RestController
@RequestMapping("/api/users")
public class UsersController implements RestControllerInterface<UserDTO> {

    public static final String URL = "/api/users";

    @Autowired
    private UserService userService;

    ....     

    @Override
    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public UserDTO update(@PathVariable("id") String id, @RequestBody UserDTO entity) {
        UserDTO userDto = userService.update(id, entity);
        return userDto;
    }

    ....
}

模拟的服务类:

package com.mycomp.mymessagesys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mycomp.mymessagesys.model.UserDTO;
import com.mycomp.mymessagesys.repository.UserDAO;

@Service
public class UserService {

    @Autowired
    private UserDAO userDao;

    ...

    public UserDTO update(String id, UserDTO entity) {
        UserDTO existingUser = userDao.getOne(id);
        if (existingUser != null) {
            existingUser.setName(entity.getName());
            existingUser.setAge(entity.getAge());
        }
        userDao.save(existingUser);
        return userDao.getOne(id);
    }

   ....

}

1 个答案:

答案 0 :(得分:1)

它不是映射,因为您作为mockito的userEntity条件的映射条件提供的when对象不等于将在{{1}中创建的userEntity对象}} 方法。虽然它们在结构上是相同的,但它们是不同的实例。

如果通过的实际UsersController.update对您无关紧要,您可以写

userEntity

让它发挥作用。

否则,如果您想匹配完全when(userService.update(eq("666"), any(UserDTO.class))).thenReturn(userEntity); ,请先在userIdentity中覆盖UserDTOequals方法,然后再定义使hashcode与另一个UserDTO相同的内容然后你可以使用

when(userService.update(eq("666"), eq(userEntity))).thenReturn(userEntity);