I want to mock and test this Java code:
public class BusterClient {
public BusterClient() {
}
@Autowired
public BusterClient(PromoCodeService promoCodeService) {
this.promoCodeService = promoCodeService;
}
private Optional<PromoCode> getPromoCode(CustomerRegistrationEvent event) {
Optional<Long> campaignId = getCampaignIdAsLong(event);
if (!event.hasPromoCode() || !campaignId.isPresent()) {
return Optional.empty();
}
return promoCodeService.findByCampaignPromoCodeIds(campaignId.get(), event.getPromoCode());
}
}
I created this test code:
@InjectMocks
private BusterClient app = new BusterClient();
@Test
public void testTrackedReferralNotMatchingPromoCode() throws Exception {
PromoCodeService o = mock(PromoCodeService.class);
when(o.findByCampaignPromoCodeIds(Long.valueOf(12), "test")).thenReturn(Optional.of(PromoCode.builder().id(Long.valueOf(1)).build()));
try {
Method method = BusterClient.class.getDeclaredMethod("getPromoCode", CustomerRegistrationEvent.class);
method.setAccessible(true);
method.invoke(app, customerRegistrationEvent);
} catch (InvocationTargetException e) {
e.getCause().printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
But I get error: promoCodeService.findByCampaignPromoCodeIds(java.lang.Long, String)" because "this.promoCodeService" is null
Do you know what is the proper way to mock PromoCodeService
service?