Spring测试-mock返回null

Spring测试-mock返回null,spring,spring-boot,junit,mocking,Spring,Spring Boot,Junit,Mocking,我对模拟方法有一个小问题。在mock应该返回预定义对象的地方,它返回null。以下是测试设置: @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class UserControllerTest { @Autowired WebApplicationContext webContext; @MockBean UserTra

我对模拟方法有一个小问题。在mock应该返回预定义对象的地方,它返回null。以下是测试设置:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserControllerTest {

@Autowired
WebApplicationContext webContext;
@MockBean
UserTransferService userTransferService;
@MockBean
UserService userService;
MockMvc mockMvc;

private User user = DummyObjects.getDummyUser();
private User modifiedUser = DummyObjects.getModifiedUser();
private UserTO userTO = DummyObjects.getDummyUserTO();
private UserTO modifiedUserTO = DummyObjects.getModifiedUserTO();

@Before
public void setUp() throws Exception {
    List<User> users = new ArrayList<>();
    users.add(modifiedUser);
    users.add(user);
    given(this.userTransferService.getTO(user)).willReturn(userTO);
    given(this.userTransferService.getObject(userTO)).willReturn(user);
    given(this.userTransferService.getTO(modifiedUser)).willReturn(modifiedUserTO);
    given(this.userTransferService.getObject(modifiedUserTO)).willReturn(modifiedUser);
    given(this.userService.findAll()).willReturn(users);
    given(this.userService.save(user)).willReturn(user);
    given(this.userService.removeById(1)).willReturn(true);
    given(this.userService.getById(1)).willReturn(user);
    given(this.userService.getById(2)).willReturn(null);
    given(this.userService.modify(modifiedUser)).willReturn(modifiedUser);
    given(this.userService.findByLogin(user.getLogin())).willReturn(user);
    given(this.userService.findByLogin("AAA")).willReturn(null);

    mockMvc = MockMvcBuilders
            .webAppContextSetup(webContext)
            .apply(springSecurity())
            .build();
}
和控制器方法:

    @RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public UserTO modifyUser(@RequestBody UserTO userTO, UsernamePasswordAuthenticationToken principal) throws  IllegalAccessException{
    User user = userTransferService.getObject(userTO);
    if (principal.getName().equals(userTO.getLogin()) || permissionService.hasPermission(principal,Permission.SU)) {
        return userTransferService.getTO(userService.modify(user));
    } else {
        throw new IllegalAccessException("You are not allowed to modify user");
    }
}

User为null,但已填充UserTO。因此,模拟方法UserTransferService.getObject(userTO)工作不正常。

userTO(或者是OrderTO?)是否定义了equals()和hashCode()方法?如果不是,那么methid作为参数接收的userTO(通过反序列化JSON创建)不可能等于测试中定义的userTO(或OrderTO?)。如果UserTO和OrderTO确实是两个独立的类,那么其中一个的实例怎么可能等于另一个的实例呢?对不起,我粘贴了错误的设置。目前它是正确的。是的,他们实现了这些方法。您在模拟中使用的
UserTO
与控制器使用的
UserTO
不同。实际上,您正在调用一个方法,这意味着
UserTO
被序列化/反序列化,使其成为不同的对象。这使得模拟非常过时,因为它不匹配,默认行为是返回
null
。基本上,它是应该工作的。@M.Deinum如果UserTO正确重写equals()和hashCode()。正如@JBNizet所暗示的那样,你的
UserTO
应该实现一个正确的
equals
hashCode
,在适当的位置它应该工作。否则,在
UserTO
的属性上编写一个更具表现力的匹配器,而不是在完整对象本身上进行匹配。UserTO(或者是OrderTO)是否定义了equals()(和hashCode())方法?如果不是,那么methid作为参数接收的userTO(通过反序列化JSON创建)不可能等于测试中定义的userTO(或OrderTO?)。如果UserTO和OrderTO确实是两个独立的类,那么其中一个的实例怎么可能等于另一个的实例呢?对不起,我粘贴了错误的设置。目前它是正确的。是的,他们实现了这些方法。您在模拟中使用的
UserTO
与控制器使用的
UserTO
不同。实际上,您正在调用一个方法,这意味着
UserTO
被序列化/反序列化,使其成为不同的对象。这使得模拟非常过时,因为它不匹配,默认行为是返回
null
。基本上,它是应该工作的。@M.Deinum如果UserTO正确重写equals()和hashCode()。正如@JBNizet所暗示的那样,你的
UserTO
应该实现一个正确的
equals
hashCode
,在适当的位置它应该工作。否则,在
UserTO
的属性上编写一个更具表现力的匹配器,而不是完整对象本身。
    @RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public UserTO modifyUser(@RequestBody UserTO userTO, UsernamePasswordAuthenticationToken principal) throws  IllegalAccessException{
    User user = userTransferService.getObject(userTO);
    if (principal.getName().equals(userTO.getLogin()) || permissionService.hasPermission(principal,Permission.SU)) {
        return userTransferService.getTO(userService.modify(user));
    } else {
        throw new IllegalAccessException("You are not allowed to modify user");
    }
}