From 5869e78f5af3ad634a2f8618d6afcce34344f4e1 Mon Sep 17 00:00:00 2001 From: Chinwat K Date: Fri, 22 Mar 2024 23:33:55 +0700 Subject: [PATCH] unit-test - coverage controller test --- .../kbazaar/cart/CartControllerTest.java | 45 +++++++++++ .../promotion/PromotionControllerTest.java | 73 ++++++++++++++++++ .../shopper/ShopperControllerTest.java | 75 +++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java create mode 100644 kbazaar/src/test/java/com/kampus/kbazaar/promotion/PromotionControllerTest.java create mode 100644 kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperControllerTest.java diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java new file mode 100644 index 0000000..e70bfda --- /dev/null +++ b/kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java @@ -0,0 +1,45 @@ +package com.kampus.kbazaar.cart; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.kampus.kbazaar.security.JwtAuthFilter; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.FilterType; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@ExtendWith(SpringExtension.class) +@WebMvcTest( + controllers = CartController.class, + excludeFilters = + @ComponentScan.Filter( + type = FilterType.ASSIGNABLE_TYPE, + classes = JwtAuthFilter.class)) +public class CartControllerTest { + + @Autowired private MockMvc mockMvc; + + @InjectMocks private CartController cartController; + + @BeforeEach + public void setup() { + MockitoAnnotations.openMocks(this); + this.mockMvc = MockMvcBuilders.standaloneSetup(cartController).build(); + } + + @Test + public void getCart_ReturnsOk() throws Exception { + mockMvc.perform(get("/api/v1/carts").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + } +} diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/promotion/PromotionControllerTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/promotion/PromotionControllerTest.java new file mode 100644 index 0000000..7c5b059 --- /dev/null +++ b/kbazaar/src/test/java/com/kampus/kbazaar/promotion/PromotionControllerTest.java @@ -0,0 +1,73 @@ +package com.kampus.kbazaar.promotion; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.kampus.kbazaar.security.JwtAuthFilter; +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.FilterType; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; + +@ExtendWith(SpringExtension.class) +@AutoConfigureMockMvc(addFilters = false) +@WebMvcTest( + controllers = PromotionController.class, + excludeFilters = + @ComponentScan.Filter( + type = FilterType.ASSIGNABLE_TYPE, + classes = JwtAuthFilter.class)) +public class PromotionControllerTest { + + @Autowired private MockMvc mockMvc; + + @MockBean private PromotionService promotionService; + + @BeforeEach + public void setup() { + MockitoAnnotations.openMocks(this); + } + + @Test + @DisplayName("should return all promotions") + public void shouldReturnAllPromotions() throws Exception { + // Given + + // When & Then + when(promotionService.getAll()).thenReturn(new ArrayList<>()); + + mockMvc.perform(get("/api/v1/promotions").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + verify(promotionService, times(1)).getAll(); + } + + @Test + @DisplayName("should return promotion") + public void shouldReturnPromotion() throws Exception { + // Given + String code = "PROMO-1"; + + // When & Then + when(promotionService.getPromotionByCode(code)).thenReturn(null); + + mockMvc.perform(get("/api/v1/promotions/" + code).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + verify(promotionService, times(1)).getPromotionByCode(code); + } +} diff --git a/kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperControllerTest.java b/kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperControllerTest.java new file mode 100644 index 0000000..ce0f50a --- /dev/null +++ b/kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperControllerTest.java @@ -0,0 +1,75 @@ +package com.kampus.kbazaar.shopper; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.kampus.kbazaar.security.JwtAuthFilter; +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.FilterType; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; + +@ExtendWith(SpringExtension.class) +@AutoConfigureMockMvc(addFilters = false) +@WebMvcTest( + controllers = ShopperController.class, + excludeFilters = + @ComponentScan.Filter( + type = FilterType.ASSIGNABLE_TYPE, + classes = JwtAuthFilter.class)) +public class ShopperControllerTest { + + @Autowired private MockMvc mockMvc; + + @MockBean private ShopperService shopperService; + + @BeforeEach + public void setup() { + MockitoAnnotations.openMocks(this); + } + + @Test + @DisplayName("should return all users") + public void getAllUsers_ShouldReturnAllUsers() throws Exception { + // Given + + // When & Then + when(shopperService.getAll()).thenReturn(new ArrayList<>()); + + mockMvc.perform(get("/api/v1/shoppers").contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + verify(shopperService, times(1)).getAll(); + } + + @Test + @DisplayName("should return shopper response") + public void shouldReturnShopperResponse() throws Exception { + // Given + String username = "cat"; + + // When & Then + when(shopperService.getByUsername(username)).thenReturn(new ShopperResponse(1L, "cat", "")); + + mockMvc.perform(get("/api/v1/shoppers/" + username).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.username").value(username)); + + verify(shopperService, times(1)).getByUsername(username); + } +}