My question is : what repository will return when object not found in junit tests.
I have test like this :
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
public class CouponServiceTestSuite {
private final static String HOME_TEAM = "home team";
private final static String AWAY_TEAM = "away team";
@Autowired
private CouponService couponService;
@MockBean
private CouponRepository couponRepository;
@MockBean
private MatchRepository matchRepository;
@Test
public void shouldThrowException() {
//Given
//When
//Then
Assertions.assertThrows(BetWinnerException.class, () -> couponService.getCoupon(-6L));
I want to mock this like :
Mockito.when(couponRepository.findById(ArgumentMatchers.anyLong()).thenReturn(null);
My service class :
@Slf4j
@RequiredArgsConstructor
@Service
public class CouponService {
private final CouponRepository couponRepository;
private final MatchRepository matchRepository;
private final CouponMapper couponMapper;
public List<CouponDto> getCoupons() {
log.debug("Getting all coupons");
List<Coupon> couponList = couponRepository.findAll();
List<CouponDto> couponDtoList = couponMapper.mapToCouponDtoList(couponList);
log.debug("Return all coupons: {}", couponDtoList);
return couponDtoList;
}
public CouponDto getCoupon(Long couponId) {
log.debug("Getting coupon by id: {}", couponId);
Coupon coupon = couponRepository.findById(couponId).orElseThrow(()
-> new BetWinnerException(BetWinnerException.ERR_COUPON_NOT_FOUND_EXCEPTION));
CouponDto couponDto = couponMapper.mapToCouponDto(coupon);
log.debug("Return coupon: {}", couponDto);
return couponDto;
}
public CouponDto createCoupon() {
log.debug("Creating new coupon");
Coupon coupon = couponRepository.save(new Coupon());
CouponDto couponDto = couponMapper.mapToCouponDto(coupon);
log.debug("Return created coupon: {}", couponDto);
return couponDto;
}
public CouponDto addMatch(Long couponId, Long matchId) {
log.debug("Add match to the coupon: {}{}", matchId, couponId);
Coupon coupon = couponRepository.findById(couponId).orElseThrow(()
-> new BetWinnerException(BetWinnerException.ERR_COUPON_NOT_FOUND_EXCEPTION));
Match match = matchRepository.findById(matchId).orElseThrow(()
-> new BetWinnerException(BetWinnerException.ERR_MATCH_NOT_FOUND_EXCEPTION));
coupon.getMatchList().add(match);
Coupon updatedCoupon = couponRepository.save(coupon);
CouponDto couponDto = couponMapper.mapToCouponDto(updatedCoupon);
log.debug("Return coupon with added match: {}", couponDto);
return couponDto;
}
public boolean deleteCoupon(Long couponId) {
log.debug("Deleting coupon id: {}", couponId);
couponRepository.deleteById(couponId);
if (couponRepository.existsById(couponId)) {
log.debug("Coupon not deleted id: {}", couponId);
return false;
} else {
log.debug("Coupon deleted id: {}", couponId);
return true;
}
}
}
I thought that it returns null but when i do like this it returns NullPointerException. My service returns BetWinnerException when object is not found. So what it will return ? How should i create this test ?
Test like this works properly but i dont want to use id = -6. I just want to mock it somehow.
Thanks.
Aucun commentaire:
Enregistrer un commentaire