Когда / thenReturn не отображается - PullRequest
0 голосов
/ 10 мая 2018

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

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

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

Тестовый файл 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 голосов
/ 11 мая 2018

Это не отображение, потому что объект userEntity, который вы задаете в качестве условия отображения для условия when mockito, не равен объекту 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);
...