From b72fa874450708ee2646369677b7af1baedb492a Mon Sep 17 00:00:00 2001 From: Denis Natusch Date: Mon, 27 Nov 2023 16:44:22 +0100 Subject: [PATCH] Use findAll in staff tests Co-authored-by: Simon Bruder --- .../StaffControllerIntegrationTests.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/test/java/catering/staff/StaffControllerIntegrationTests.java b/src/test/java/catering/staff/StaffControllerIntegrationTests.java index c7275b2..f6d5265 100644 --- a/src/test/java/catering/staff/StaffControllerIntegrationTests.java +++ b/src/test/java/catering/staff/StaffControllerIntegrationTests.java @@ -10,6 +10,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.assertj.core.api.Assertions.assertThat; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -43,6 +44,13 @@ class StaffControllerIntegrationTests { defaultEmployeeId = defaultEmployee.getId(); } + @AfterEach + void tearDown() throws Exception { + staffManagement.delete(defaultEmployeeId); + defaultEmployee = null; + defaultEmployeeId = null; + } + @Test @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") void viewStaff() throws Exception { @@ -62,8 +70,13 @@ class StaffControllerIntegrationTests { @Test @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") void addStaff() throws Exception { - // Needs to be updated if findAll() returns something useful + assertThat(staffManagement.findAll().stream()) + .extracting("name") + .doesNotContain("Karl Baum"); mvc.perform(createStaff).andExpect(redirectedUrl("/staff")); + assertThat(staffManagement.findAll().stream()) + .extracting("name") + .contains("Karl Baum"); } @Test @@ -81,10 +94,13 @@ class StaffControllerIntegrationTests { @Test @WithMockUser(username = "admin", password = "admin", roles = "ADMIN") void removeStaff() throws Exception { + assertThat(staffManagement.findAll().stream()) + .extracting("name") + .contains("Dieter Baum"); mvc.perform(post("/staff/remove").param("id", defaultEmployeeId.toString())); - // this should be replaced once we have proper access to all employees - mvc.perform(get("/staff/edit/" + defaultEmployeeId.toString())) - .andExpect(status().isInternalServerError()); + assertThat(staffManagement.findAll().stream()) + .extracting("name") + .doesNotContain("Dieter Baum"); } @Test @@ -100,7 +116,7 @@ class StaffControllerIntegrationTests { mvc.perform(get("/staff")) .andExpect(status().is3xxRedirection()) .andExpect(header().string(HttpHeaders.LOCATION, endsWith("/login"))); - mvc.perform(get("/staff/edit/1")) + mvc.perform(get("/staff/edit/" + defaultEmployeeId)) .andExpect(status().is3xxRedirection()) .andExpect(header().string(HttpHeaders.LOCATION, endsWith("/login"))); }