Мой вопрос: какой репозиторий вернет, если объект не найден в тестах junit.
У меня есть такой тест:
@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));
Я хочу издеваться над этим, например:
Mockito.when(couponRepository.findById(ArgumentMatchers.anyLong()).thenReturn(null);
Мой класс обслуживания:
@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;
}
}
}
Я думал, что он возвращает null, но когда я это делаю, он возвращает NullPointerException. Моя служба возвращает BetWinnerException, если объект не найден. Так что он вернет? Как мне создать этот тест?
Подобный тест работает правильно, но я не хочу использовать id = -6. Я просто хочу как-нибудь поиздеваться.