Issue
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.
Solution
You are mocking couponRepository
but using couponService
and as there is no code shown how that is initialised in your test, it is hard to tell, where the problem is.
Now that you updated your question, the answer is quite obvious:
- in the service the code expects
couponRepository.findById()
to return anOptional
so that it can throw the exception if that is empty - mocked beans are 'normal'
Mockito
mocks that try to return a useful result; for collection this is an empty collection, for objects this is null - usually - TIL that Mockito 2 will support what you expect: https://www.baeldung.com/mockito-2-java-8#return-default-values-for-optional-and-stream
- this article also shows how to make Mockito 1 return the empty
Optional
- you tried to make it return
null
but you actually needOptional.empty()
Or did I misunderstand how you actually test it and what your problem is?
- Do you get a
NullPointerException
in the service as I understood? - Or do you already use Mockito 2 and have another issue?
Answered By - codesimplicity