When/thenReturn 未映射
When/thenReturn not mapped
我不明白为什么我的 when/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());
}
}
测试用户控制器class:
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;
}
....
}
嘲笑的服务class:
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);
}
....
}
它不是映射,因为您作为 mockito 的 when
条件的映射条件提供的 userEntity
对象不等于将在 userEntity
中创建的对象=15=]方法。尽管它们在结构上相同,但它们是不同的实例。
如果实际传递的userEntity
对你来说无所谓,你可以写
when(userService.update(eq("666"), any(UserDTO.class))).thenReturn(userEntity);
让它发挥作用。
否则,如果你想精确匹配 userIdentity
,那么首先通过覆盖 UserDTO
中的 equals
和 hashcode
方法来定义使一个 UserDTO
与另一个相同的原因] 然后你可以使用
when(userService.update(eq("666"), eq(userEntity))).thenReturn(userEntity);
我不明白为什么我的 when/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());
}
}
测试用户控制器class:
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;
}
....
}
嘲笑的服务class:
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);
}
....
}
它不是映射,因为您作为 mockito 的 when
条件的映射条件提供的 userEntity
对象不等于将在 userEntity
中创建的对象=15=]方法。尽管它们在结构上相同,但它们是不同的实例。
如果实际传递的userEntity
对你来说无所谓,你可以写
when(userService.update(eq("666"), any(UserDTO.class))).thenReturn(userEntity);
让它发挥作用。
否则,如果你想精确匹配 userIdentity
,那么首先通过覆盖 UserDTO
中的 equals
和 hashcode
方法来定义使一个 UserDTO
与另一个相同的原因] 然后你可以使用
when(userService.update(eq("666"), eq(userEntity))).thenReturn(userEntity);