Spring boot, junit, mocking - null zamiast obiektu

0

Witam mam mały problem z mockowanym obiektem w tescie springowej aplikacji. Mockowany serwis zwraca null zamiast obiektu zadanego w przygotowaniu do wywołania testu.Oto set up:

@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();
    }

Test :

    @Test
    @WithMockUser(username = "admin",authorities = {"SU"})
    public void shouldModifyUserWithSuAuthority() throws Exception {
        String modifiedUserToJSON = TestingUtility.asJsonString(modifiedUserTO);
        mockMvc.perform(put("/api/user/")
                .content(modifiedUserToJSON)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .with(csrf().asHeader()))
                .andExpect(status().is(200))
                .andExpect(content().json(modifiedUserToJSON));
    }

Oraz metoda kotrollera:

    @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");
        }
    }

W linii kontrollera gdzie dodaje do zmiennej User user zwracana wartosc z serwisu ( która powinna być zmockowana) dostaje null i odpowiednio dostaje null pointer exception. Macie jakieś pomysły lub rady jak testować taka funkcjonalnosc?

Dzięki z góry!

1

Użyj userTransferService.getObject(any()). W tym teście obiekt user w ogóle Cię nie interesuje. Może w innych testach tak, ale w teście shouldModifyUserWithSuAuthority na pewno nie. W innych przypadkach może być konieczne użycie np. Answer. Ogólnie problem stanowi to, że obiekt, który kontroler odszyfrowuje z Jsona, to nie ten sam obiekt, który mu przekazujesz w metodzie testowej.

1 użytkowników online, w tym zalogowanych: 0, gości: 1