-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unit-test - coverage controller test
- Loading branch information
1 parent
dc742f8
commit 5869e78
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
kbazaar/src/test/java/com/kampus/kbazaar/cart/CartControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
kbazaar/src/test/java/com/kampus/kbazaar/promotion/PromotionControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
kbazaar/src/test/java/com/kampus/kbazaar/shopper/ShopperControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |