From 50da15dfd0a596514e26d20b476875afbbf8672d Mon Sep 17 00:00:00 2001 From: Lara Blersch <lb210@hdm-stuttgart.de> Date: Mon, 15 Jan 2024 18:50:31 +0100 Subject: [PATCH] implemented unit test for wishlistController --- .../controllers/WishListControllerTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/java/hdm/mi/growbros/controllers/WishListControllerTest.java diff --git a/src/test/java/hdm/mi/growbros/controllers/WishListControllerTest.java b/src/test/java/hdm/mi/growbros/controllers/WishListControllerTest.java new file mode 100644 index 0000000..cce2ecf --- /dev/null +++ b/src/test/java/hdm/mi/growbros/controllers/WishListControllerTest.java @@ -0,0 +1,69 @@ +package hdm.mi.growbros.controllers; + +import hdm.mi.growbros.config.SpringSecurityTestConfig; +import hdm.mi.growbros.models.plant.Plant; +import hdm.mi.growbros.service.WishListService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@SpringBootTest(classes = SpringSecurityTestConfig.class) +@AutoConfigureMockMvc +@ActiveProfiles("test") +public class WishListControllerTest { + + @Autowired + private MockMvc mvc; + + @MockBean + private WishListService wishlistService; + + @Test + @WithMockUser + void wishlistEntries_shouldBeOkJson_containList() throws Exception { + when(wishlistService.getWishedPlants(any(), any())).thenReturn(List.of(Plant.builder().name("Plant").build())); + + mvc.perform(get("/api/v1/wishlist").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.size()").value(1)) + .andDo(print()); + + verify(wishlistService).getWishedPlants(any(), any()); + } + + @Test + @WithMockUser + void creatingEntry_shouldBe201_andContainPlant() throws Exception { + + mvc.perform(post("/api/v1/wishlist/add/0").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()) + .andDo(print()); + + verify(wishlistService).addPlantToWishList(anyLong(), any()); + } + + @Test + @WithMockUser + void deletingEntry_shouldBe204() throws Exception { + mvc.perform(delete("/api/v1/wishlist/remove/1").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNoContent()) + .andDo(print()); + } +} \ No newline at end of file -- GitLab