-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat::product controller testCode 추가
- Loading branch information
1 parent
45d60da
commit b32d9ec
Showing
7 changed files
with
149 additions
and
18 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
|
@@ -67,7 +67,7 @@ public void setup() { | |
void 로그인() throws Exception { | ||
|
||
MemberDto memberDto = MemberDto.of("testID", "testPassword"); | ||
MemberDto memberInfo = new MemberDto(1, | ||
MemberDto memberInfo = new MemberDto(1L, | ||
"test01", | ||
"[email protected]", | ||
"", | ||
|
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
class MemberTest { | ||
|
||
private Integer memberNo; | ||
private Long memberNo; | ||
private String id; | ||
private String email; | ||
private String password; | ||
|
@@ -20,7 +20,7 @@ class MemberTest { | |
|
||
@BeforeEach | ||
void 테스트준비() { | ||
memberNo = 1; | ||
memberNo = 1L; | ||
id = "user123"; | ||
email = "[email protected]"; | ||
password = "password123"; | ||
|
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 |
---|---|---|
|
@@ -81,7 +81,7 @@ public void init() { | |
/** given **/ | ||
MemberDto loginDto = MemberDto.of("test03", "1234qwer"); | ||
Member mockLoginEntity = Member.loginMember(loginDto.id(), loginDto.password()); | ||
Member mockEntity = Member.of(502, loginDto.id(), "[email protected]", "0eb9de69892882d54516e03e30098354a2e39cea36adab275b6300c737c942fd", null, null); | ||
Member mockEntity = Member.of(502L, loginDto.id(), "[email protected]", "0eb9de69892882d54516e03e30098354a2e39cea36adab275b6300c737c942fd", null, null); | ||
String mockToken = "dummy_token"; | ||
|
||
/** stub **/ | ||
|
@@ -105,13 +105,13 @@ public void init() { | |
void 토큰_발급_저장_테스트() { | ||
|
||
/** given **/ | ||
Member memberEntity = Member.of(502, "test03", null, null, null, null); | ||
Member memberEntity = Member.of(502L, "test03", null, null, null, null); | ||
String mockToken = "mockToken"; | ||
Auth auth = Auth.jwt(null, 502, mockToken); | ||
Auth auth = Auth.jwt(null, 502L, mockToken); | ||
|
||
/** stub **/ | ||
when(jwt.generateToken(anyString())).thenReturn(mockToken); | ||
when(authRepository.findOneByMemberNo(anyInt())).thenReturn(auth); | ||
when(authRepository.findOneByMemberNo(anyLong())).thenReturn(auth); | ||
|
||
/** when **/ | ||
String token = jwt.generateToken(memberEntity.getId()); //mockToken return 정상 수행 확인 | ||
|
@@ -136,7 +136,7 @@ public void init() { | |
|
||
/** given **/ | ||
String id = "test01"; | ||
Member memberEntity = Member.of(51, "test01", null, null, null, null); | ||
Member memberEntity = Member.of(51L, "test01", null, null, null, null); | ||
|
||
/** stub **/ | ||
when(memberRepository.findOneById(anyString())).thenReturn(memberEntity); | ||
|
@@ -175,7 +175,7 @@ public void init() { | |
|
||
/** given **/ | ||
String email = "[email protected]"; | ||
Member memberEntity = Member.of(51, "test01", "[email protected]", null, null, null); | ||
Member memberEntity = Member.of(51L, "test01", "[email protected]", null, null, null); | ||
|
||
/** stub **/ | ||
when(memberRepository.findOneByEmail(anyString())).thenReturn(memberEntity); | ||
|
131 changes: 131 additions & 0 deletions
131
module-api/src/test/java/com/kernel360/product/controller/ProductControllerTest.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,131 @@ | ||
package com.kernel360.product.controller; | ||
|
||
import com.kernel360.product.dto.ProductDto; | ||
import com.kernel360.product.entity.Product; | ||
import com.kernel360.product.repository.ProductRepository; | ||
import com.kernel360.product.service.ProductService; | ||
import com.navercorp.fixturemonkey.FixtureMonkey; | ||
import com.navercorp.fixturemonkey.api.introspector.*; | ||
import net.jqwik.api.Arbitraries; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
import static org.mockito.Mockito.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@WebMvcTest(ProductController.class) | ||
class ProductControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
private FixtureMonkey fixtureMonkey; | ||
@MockBean | ||
private ProductService productService; | ||
@MockBean | ||
private ProductRepository productRepository; | ||
|
||
@BeforeEach | ||
void 준비(WebApplicationContext webApplicationContext) { | ||
fixtureMonkey = FixtureMonkey.builder() | ||
.objectIntrospector(new FailoverIntrospector( | ||
Arrays.asList( | ||
FieldReflectionArbitraryIntrospector.INSTANCE, | ||
ConstructorPropertiesArbitraryIntrospector.INSTANCE, | ||
BeanArbitraryIntrospector.INSTANCE, | ||
BuilderArbitraryIntrospector.INSTANCE | ||
) | ||
)) | ||
.build(); | ||
|
||
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); | ||
} | ||
|
||
@Test | ||
void 제품목록요청_왔을때_200응답과_리스폰스가_잘반환되는지() throws Exception { | ||
// given | ||
List<Product> products = fixtureMonkey.giveMeBuilder(Product.class) | ||
.sampleList(5); | ||
|
||
List<ProductDto> expectedDtos = products.stream() | ||
.map(ProductDto::from) | ||
.collect(Collectors.toList()); | ||
|
||
//when | ||
when(productService.getProductList()).thenReturn(products); | ||
|
||
// then | ||
mockMvc.perform(get("/products")) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$", hasSize(5))); | ||
|
||
verify(productService, times(1)).getProductList(); | ||
} | ||
|
||
@Test | ||
void 제품아이디로_제품조회요청이왔을때_200응답과_리스폰스가_잘반환되는지() throws Exception { | ||
// given | ||
Product mockProduct = fixtureMonkey.giveMeBuilder(Product.class) | ||
.set("productId", 1L) | ||
.sample(); | ||
|
||
when(productService.getProductById(1L)).thenReturn(Optional.of(mockProduct)); | ||
|
||
// when & then | ||
mockMvc.perform(get("/product/{id}", 1L)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(jsonPath("$.productName", is(mockProduct.getProductName()))) | ||
.andExpect(jsonPath("$.barcode", is(mockProduct.getBarcode()))) | ||
.andExpect(jsonPath("$.description", is(mockProduct.getDescription()))) | ||
.andExpect(jsonPath("$.declareNo", is(mockProduct.getDeclareNo()))) | ||
.andExpect(jsonPath("$.isViolation", is(mockProduct.getIsViolation()))) | ||
.andExpect(jsonPath("$.viewCount", is(mockProduct.getViewCount()))); | ||
|
||
verify(productService).getProductById(1L); | ||
|
||
} | ||
|
||
@Test | ||
void 키워드로_검색했을때_200코드와_리스폰스가_잘반환되는지() throws Exception { | ||
// given | ||
List<ProductDto> products = new ArrayList<>(); | ||
String keyword = "sample"; | ||
for (int i = 0; i < 5; i++) { | ||
ProductDto productDto = fixtureMonkey.giveMeBuilder(ProductDto.class) | ||
.set("productName", keyword + i) | ||
.sample(); | ||
products.add(productDto); | ||
} | ||
|
||
//when | ||
when(productService.getProductListByKeyword(keyword)).thenReturn(products); | ||
|
||
//then | ||
mockMvc.perform(get("/products/search").param("keyword", keyword)) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$", hasSize(5))); | ||
|
||
verify(productService, times(1)).getProductListByKeyword(keyword); | ||
|
||
} | ||
|
||
} |