Skip to content
Snippets Groups Projects
Commit 50da15df authored by Blersch Lara's avatar Blersch Lara
Browse files

implemented unit test for wishlistController

parent aff50458
No related branches found
No related tags found
1 merge request!51Wishlist unittests
Pipeline #57448 passed
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment