Home > Back-end >  null exception for mocked service
null exception for mocked service

Time:10-07

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?

CodePudding user response:

You must do something like this

    @Mock
    private PromoCodeService promoCodeService;
    @InjectMocks
    private BusterClient app;
    
      @BeforeEach
      void setUp() {
    
        MockitoAnnotations.initMocks(this);
      }
    
    @Test
    public void testTrackedReferralNotMatchingPromoCode() throws Exception {
        
        when(promoCodeService.findByCampaignPromoCodeIds(Long.valueOf(12), "test")).thenReturn(Optional.of(PromoCode.builder().id(Long.valueOf(1)).build()));
    
        ...
// Test a public method and not a private method. Use one public method that in turn will call your "getPromoCode".
    }

CodePudding user response:

Try to inject your mocked object into the client you are creating, and create the client in the test method instead of making it class variable

var app = new BusterClient(o); //where o is the object you created with mockito
  • Related