From 67b2c3ae4f1fde8ea7c097f8fa46c8ac1d0c681f Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 22 May 2020 13:23:14 +0200 Subject: [PATCH] findguests should be tested and imports should be fixed --- .../controller/GuestControllerTests.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java index e24721a..9bc0753 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/GuestControllerTests.java @@ -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)); + } }