mercredi 29 mai 2019

Mockito default behaviour and custom behaviour with methods with identical return types

Supossing I have the following code to test UserController by mocking UserService:

public class UserControllerTest {
    private final List<User> users1 = new ArrayList<>();
    private final List<User> users2 = new ArrayList<>();

    @Before
    public void initUsers() {
        User user = new User();
        user.setId(1L);
        users1.add(user);

        User user = new User();
        user.setId(2L);
        users2.add(user);
    }

    @Test
    public void testList() throws Exception {
        UserService userService = mock(UserService.class); //line1

        when(userService.findAll1()).thenReturn(users1); //line2
        when(userService.findAll2()).thenReturn(users2); //line3


        SingerController singerController = new SingerController();
        ReflectionTestUtils.setField(singerController, "singerService", singerService);
        List<User> users3 = singerController.findAll1(); //line4
        List<User> users4 = singerController.findAll2(); //line5

        ...
    }
}

I have the following doubts:

  1. When the line1 is reached, what would be the default behaviour for userService.findAll1() and userService.findAll2()?
  2. When the line3 is reached, as userService.findAll1() and userService.findAll2() return the same result type (List<User>). Will the line3 override the behaviour defined in line2? I mean, will userService.findAll1() return users2 instead of users1?




Aucun commentaire:

Enregistrer un commentaire