Когда /thenReturn не отображается

Я не могу понять, почему мой when/thenReturn не сопоставлен с осмеянным компонентом.

Во время отладки я вижу, что "userDto" после строки "UserDTO userDto = userService.update(id, entity);" в тестируемом контроллере равно нулю.

Есть идеи, что я делаю не так?

Тестовый файл 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 ответ

Решение

Это не отображение, потому что userEntity объект, который вы даете в качестве условия сопоставления для Mockito's when условие не равно userEntity объект, который будет создан внутри UsersController.update метод. Хотя они структурно одинаковы, они разные экземпляры.

Если фактический 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);
Другие вопросы по тегам