Service TestCode에서 save 메소드 트러블슈팅

kinkin_a·2025년 4월 9일

내일배움캠프 TIL

목록 보기
90/100

1차시도

Coupon coupon = new Coupon("coupon","description",10.0,expiredAt,10);
given(couponRepository.save(coupon).willReturn(coupon); 

실패 ErrorMessage

Strict stubbing argument mismatch. Please check:
 - this invocation of 'save' method:
    couponRepository.save(
    com.example.auctionmarket.domain.coupon.entity.Coupon@7f5eae0f

실제 createCoupon메소드에서 생성된 coupon객체와 test코드에서 생성된 coupon객체가 다르기 때문.

2차시도

given(couponRepository.save(any(Coupon.class).willReturn(coupon); 

실패 Error Message

 required: S
  found:    Matcher<Coupon>
  reason: inference variable S has incompatible bounds
    lower bounds: Coupon
    lower bounds: Matcher<T#2>
  where S,T#1,ID,T#2 are type-variables:
    S extends Coupon declared in method <S>save(S)
    T#1 extends Object declared in interface CrudRepository
    ID extends Object declared in interface CrudRepository
    T#2 extends Object declared in method <T#2>any(Class<T#2>) 

여기서 한참 헤매다가 겨우 답을 알아냄

given(couponRepository.save(Mockito.<Coupon>any())).willReturn(coupon);     

알고보니

JpaRepository의 save메소드는

<S extends T> S save(S entity);

이런 식으로 돼서 S안에 S가 있는 구조라 예외적으로 any만으론 안된다고 함.

0개의 댓글