findguests should be tested and imports should be fixed

This commit is contained in:
Tommaso Rodolfo Masera 2020-05-22 13:23:14 +02:00
parent 420fed7f88
commit 67b2c3ae4f

View file

@ -16,6 +16,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.test.context.support.WithMockUser;
@ExtendWith(MockitoExtension.class)
@WithMockUser(username = "user")
@ -59,11 +60,11 @@ public class GuestControllerTests {
assertThat(guestController.findAll()).isSameAs(List.of(host, guest));
}
@DisplayName("Check that we get empty host list")
@DisplayName("Check that the host list is empty")
@Test
public void findHostsEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user2");
Mockito.doReturn(List.of()).when(userRepository.findByUsername("user2"));
when(mockPrincipal.getName()).thenReturn("user");
Mockito.doReturn(List.of()).when(userRepository.findByUsername("user"));
assertThat(guestController.findHosts(mockPrincipal).isEmpty());
}
@ -78,4 +79,24 @@ public class GuestControllerTests {
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")
@Test
public void findGuestsEmptyTest() {
when(mockPrincipal.getName()).thenReturn("user");
Mockito.doReturn(List.of()).when(userRepository.findByUsername("user"));
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));
}
}