-
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.
Merge pull request #253 from Team-BC-1/test/Coupon
쿠폰 관련 테스트 코드 추가
- Loading branch information
Showing
14 changed files
with
746 additions
and
69 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
95 changes: 95 additions & 0 deletions
95
src/test/java/bc1/gream/domain/coupon/controller/CouponControllerTest.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,95 @@ | ||
package bc1.gream.domain.coupon.controller; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import bc1.gream.domain.buy.service.query.BuyQueryService; | ||
import bc1.gream.domain.coupon.entity.Coupon; | ||
import bc1.gream.domain.coupon.service.qeury.CouponQueryService; | ||
import bc1.gream.global.security.UserDetailsImpl; | ||
import bc1.gream.global.security.WithMockCustomUser; | ||
import bc1.gream.test.CouponTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
@WebMvcTest(controllers = CouponController.class) | ||
@WithMockCustomUser | ||
@ActiveProfiles("test") | ||
class CouponControllerTest implements CouponTest { | ||
|
||
@Autowired | ||
protected ObjectMapper objectMapper; | ||
@MockBean | ||
CouponQueryService couponQueryService; | ||
@MockBean | ||
BuyQueryService buyQueryService; | ||
@Autowired | ||
private MockMvc mockMvc; | ||
@Autowired | ||
private WebApplicationContext context; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(context) | ||
.alwaysDo(print()).build(); | ||
} | ||
|
||
@Test | ||
void 로그인_한_유저가_사용가능한_쿠폰을_조회하는_컨트롤러_기능_성공_테스트() throws Exception { | ||
|
||
// given | ||
List<Coupon> couponList = new ArrayList<>(); | ||
for (int i = 0; i < 5; i++) { | ||
couponList.add((i % 2 == 0) ? TEST_COUPON_FIX : TEST_COUPON_RATE); | ||
} | ||
|
||
given(couponQueryService.availableCouponList(any(UserDetailsImpl.class))).willReturn(couponList); | ||
|
||
// when - then | ||
mockMvc.perform(get("/api/coupons")) | ||
.andExpectAll( | ||
status().isOk(), | ||
jsonPath("$.code").value(0), | ||
jsonPath("$.message").value("정상 처리 되었습니다"), | ||
jsonPath("$.data.size()").value(5), | ||
jsonPath("$.data[0].discountType").value("FIX"), | ||
jsonPath("$.data[1].discountType").value("RATE") | ||
); | ||
} | ||
|
||
@Test | ||
void 로그인_한_유저가_이미_사용한_쿠폰을_조회하는_컨트롤러_기능_성공_테스트() throws Exception { | ||
|
||
// given | ||
List<Coupon> couponList = new ArrayList<>(); | ||
for (int i = 0; i < 5; i++) { | ||
couponList.add((i % 2 == 0) ? TEST_COUPON_RATE_USED : TEST_COUPON_FIX_USED); | ||
} | ||
given(couponQueryService.unavailableCouponList(any(UserDetailsImpl.class))).willReturn(couponList); | ||
|
||
// when - then | ||
mockMvc.perform(get("/api/coupons/used")) | ||
.andExpectAll( | ||
status().isOk(), | ||
jsonPath("$.code").value(0), | ||
jsonPath("$.message").value("정상 처리 되었습니다"), | ||
jsonPath("$.data.size()").value(5), | ||
jsonPath("$.data[0].discountType").value("RATE"), | ||
jsonPath("$.data[1].discountType").value("FIX") | ||
); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/test/java/bc1/gream/domain/coupon/provider/CouponProviderTest.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,54 @@ | ||
package bc1.gream.domain.coupon.provider; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
import bc1.gream.domain.admin.dto.request.AdminCreateCouponRequestDto; | ||
import bc1.gream.domain.coupon.entity.Coupon; | ||
import bc1.gream.domain.coupon.entity.DiscountType; | ||
import bc1.gream.domain.coupon.service.command.CouponCommandService; | ||
import bc1.gream.domain.user.entity.User; | ||
import bc1.gream.domain.user.repository.UserRepository; | ||
import bc1.gream.test.CouponTest; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class CouponProviderTest implements CouponTest { | ||
|
||
@InjectMocks | ||
private CouponProvider couponProvider; | ||
@Mock | ||
private UserRepository userRepository; | ||
@Mock | ||
private CouponCommandService couponCommandService; | ||
|
||
@Test | ||
void 관리자가_사용자에게_쿠폰을_등록해주는_Provider_성공_테스트() { | ||
|
||
// given | ||
AdminCreateCouponRequestDto requestDto = AdminCreateCouponRequestDto.builder() | ||
.name("TEST COUPON") | ||
.discountType(DiscountType.FIX) | ||
.discount(500L) | ||
.userLoginId(TEST_USER_LOGIN_ID) | ||
.build(); | ||
|
||
given(userRepository.findByLoginId(any(String.class))).willReturn(Optional.of(TEST_USER)); | ||
given(couponCommandService.createCoupon(any(User.class), any(AdminCreateCouponRequestDto.class))).willReturn(TEST_COUPON_FIX); | ||
|
||
// when | ||
Coupon resultCoupon = couponProvider.createCoupon(requestDto); | ||
|
||
// then | ||
assertThat(resultCoupon.getName()).isEqualTo(requestDto.name()); | ||
assertThat(resultCoupon.getDiscount()).isEqualTo(requestDto.discount()); | ||
assertThat(resultCoupon.getDiscountType()).isEqualTo(requestDto.discountType()); | ||
|
||
} | ||
} |
Oops, something went wrong.