Skip to content

Commit

Permalink
[fix] IssueCouponServiceTest 수정
Browse files Browse the repository at this point in the history
-  비관적 락 적용에 따른 테스트 코드 수정
  • Loading branch information
kimhyun5u committed Aug 15, 2024
1 parent bb1650e commit f770f0d
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void testIssueCoupon() {
Customer fakeCustomer = createCustomer(fakeCustomerId);
CouponIssuance fakeCouponIssuance = createCouponIssuance(fakeCouponId, fakeCoupon, fakeCustomer);
given(customerRepository.findById(fakeCustomerId)).willReturn(Optional.of(fakeCustomer));
given(couponRepository.findById(fakeCouponId)).willReturn(Optional.of(fakeCoupon));
given(couponRepository.findByIdWithPessimisticLock(fakeCouponId)).willReturn(Optional.of(fakeCoupon));
given(couponIssuanceRepository.save(any(CouponIssuance.class))).willReturn(fakeCouponIssuance);

IssueCouponCommand cmd = new IssueCouponCommand(fakeCustomerId, fakeCouponId);
Expand All @@ -65,7 +65,7 @@ void testIssueCoupon() {
// then
assertEquals(fakeCouponIssuanceId, saveId);
verify(customerRepository).findById(fakeCustomerId);
verify(couponRepository).findById(fakeCouponId);
verify(couponRepository).findByIdWithPessimisticLock(fakeCouponId);
verify(couponIssuanceRepository).save(any(CouponIssuance.class));
}

Expand Down Expand Up @@ -94,14 +94,14 @@ void testIssueCouponFailWithNotExistCoupon() {
Long fakeCouponId = 1L;
Customer fakeCustomer = createCustomer(fakeCustomerId);
given(customerRepository.findById(fakeCustomerId)).willReturn(Optional.of(fakeCustomer));
given(couponRepository.findById(fakeCouponId)).willReturn(Optional.empty());
given(couponRepository.findByIdWithPessimisticLock(fakeCouponId)).willReturn(Optional.empty());

IssueCouponCommand cmd = new IssueCouponCommand(fakeCustomerId, fakeCouponId);

// when & then
assertThrows(InvalidICreationIssuanceException.class, () -> issueCouponService.issueCoupon(cmd));
verify(customerRepository).findById(fakeCustomerId);
verify(couponRepository).findById(fakeCouponId);
verify(couponRepository).findByIdWithPessimisticLock(fakeCouponId);
verify(couponIssuanceRepository, never()).save(any(CouponIssuance.class));
}

Expand All @@ -116,14 +116,14 @@ void testIssueCouponFailWithExpiredCoupon() {
fakeCoupon.setExpiredAt(LocalDateTime.now().minusDays(1));
Customer fakeCustomer = createCustomer(fakeCustomerId);
given(customerRepository.findById(fakeCustomerId)).willReturn(Optional.of(fakeCustomer));
given(couponRepository.findById(fakeCouponId)).willReturn(Optional.of(fakeCoupon));
given(couponRepository.findByIdWithPessimisticLock(fakeCouponId)).willReturn(Optional.of(fakeCoupon));

IssueCouponCommand cmd = new IssueCouponCommand(fakeCustomerId, fakeCouponId);

// when & then
assertThrows(ExpiredCouponException.class, () -> issueCouponService.issueCoupon(cmd));
verify(customerRepository).findById(fakeCustomerId);
verify(couponRepository).findById(fakeCouponId);
verify(couponRepository).findByIdWithPessimisticLock(fakeCouponId);
verify(couponIssuanceRepository, never()).save(any(CouponIssuance.class));
}

Expand All @@ -138,15 +138,13 @@ void testIssueCouponFailWithInsufficientCouponQuantity() {
Customer fakeCustomer = createCustomer(fakeCustomerId);
fakeCoupon.setQuantity(0); // 수량 부족 시나리오 적용
given(customerRepository.findById(fakeCustomerId)).willReturn(Optional.of(fakeCustomer));
given(couponRepository.findById(fakeCouponId)).willReturn(Optional.of(fakeCoupon));
given(couponIssuanceRepository.save(any(CouponIssuance.class))).willThrow(
new InsufficientCouponQuantityException("the quantity of coupon is insufficient"));
given(couponRepository.findByIdWithPessimisticLock(fakeCouponId)).willReturn(Optional.of(fakeCoupon));
IssueCouponCommand cmd = new IssueCouponCommand(fakeCustomerId, fakeCouponId);

// when & then
assertThrows(InsufficientCouponQuantityException.class, () -> issueCouponService.issueCoupon(cmd));
verify(customerRepository).findById(fakeCustomerId);
verify(couponRepository).findById(fakeCouponId);
verify(couponIssuanceRepository).save(any(CouponIssuance.class));
verify(couponRepository).findByIdWithPessimisticLock(fakeCouponId);
verify(couponIssuanceRepository, never()).save(any(CouponIssuance.class));
}
}

0 comments on commit f770f0d

Please sign in to comment.