tests done so far should be fixed

This commit is contained in:
Tommaso Rodolfo Masera 2020-05-22 14:29:17 +02:00
parent 67b2c3ae4f
commit 64007d1b5c

View file

@ -14,7 +14,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.context.support.WithMockUser;
@ -43,60 +42,55 @@ public class GuestControllerTests {
@Test @Test
public void findAllEmptyTest() { public void findAllEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); when(userRepository.findByUsername("user")).thenReturn(user);
List<UserResponse> l = guestController.findAll(); List<UserResponse> l = guestController.findAll();
assertThat(l.isEmpty()); assertThat(l.isEmpty());
} }
@DisplayName("Check that the list contains the elements added") @DisplayName(
"Check that the list contains the elements added and that hosts and guests are returned properly")
@Test @Test
public void findAllTest() { public void findAllTest() {
User host = new User(); User host1 = new User();
User guest = new User(); User host2 = new User();
User guest1 = new User();
User guest2 = new User();
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user); when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user);
Mockito.doReturn(List.of(host, guest)).when(userRepository.findByUsername("user")); when(userRepository.findAll()).thenReturn(List.of(host1, host2, guest1, guest2));
assertThat(guestController.findAll()).isSameAs(List.of(host, guest)); assertThat(guestController.findAll())
.isSameAs(
List.of(
UserResponse.fromUser(host1),
UserResponse.fromUser(host2),
UserResponse.fromUser(guest1),
UserResponse.fromUser(guest2)));
when(guestController.findHosts(mockPrincipal))
.thenReturn(List.of(UserResponse.fromUser(host1), UserResponse.fromUser(host2)));
assertThat(guestController.findHosts(mockPrincipal))
.isSameAs(List.of(UserResponse.fromUser(host1), UserResponse.fromUser(host2)));
when(guestController.findGuests(mockPrincipal))
.thenReturn(List.of(UserResponse.fromUser(guest1), UserResponse.fromUser(guest2)));
assertThat(guestController.findGuests(mockPrincipal))
.isSameAs(List.of(UserResponse.fromUser(guest1), UserResponse.fromUser(guest2)));
} }
@DisplayName("Check that the host list is empty") @DisplayName("Check that the host list is empty")
@Test @Test
public void findHostsEmptyTest() { public void findHostsEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); when(userRepository.findByUsername("user")).thenReturn(user);
assertThat(guestController.findHosts(mockPrincipal).isEmpty()); assertThat(guestController.findHosts(mockPrincipal).isEmpty());
} }
@DisplayName("Check that the host list contains the hosts")
@Test
public void findHostsTest() {
User host1 = new User();
User host2 = new User();
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user);
Mockito.doReturn(List.of(host1, host2)).when(userRepository.findByUsername("user"));
assertThat(guestController.findHosts(mockPrincipal)).isSameAs(List.of(host1, host2));
}
@DisplayName("Check that the guest list is empty") @DisplayName("Check that the guest list is empty")
@Test @Test
public void findGuestsEmptyTest() { public void findGuestsEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user"); when(mockPrincipal.getName()).thenReturn("user");
Mockito.doReturn(List.of()).when(userRepository.findByUsername("user")); when(userRepository.findByUsername("user")).thenReturn(user);
assertThat(guestController.findGuests(mockPrincipal).isEmpty()); assertThat(guestController.findGuests(mockPrincipal).isEmpty());
} }
@DisplayName("Check that the guest list contains the guests")
@Test
public void findGuestsTest() {
User guest1 = new User();
User guest2 = new User();
when(mockPrincipal.getName()).thenReturn("user");
when(userRepository.findByUsername(mockPrincipal.getName())).thenReturn(this.user);
Mockito.doReturn(List.of(guest1, guest2)).when(userRepository.findByUsername("user"));
assertThat(guestController.findGuests(mockPrincipal)).isSameAs(List.of(guest1, guest2));
}
} }