Below is my AppleServiceImpl class and a test class AppleServiceImpl which I created.
During my testing,
- method
creditCheckgets called - we call the class and method
appleService.creditCheck(request, response);to test. - when credit check is called,the
appleHelperobject is null.doesnt get initalised. - How do I mock it or work around it to get testing done?
public class AppleServiceImpl extends GenericService implements ApplicationService {
@Autowired
AppleHelper appleHelper;
public void creditCheck(ApplicationRequest request, ResponseMessage<ApplicationResponse> response) {
appleHelper.test123(request, response);
}
}
public class AppleServiceImplTest {
@Spy
AppleServiceImpl appleService= new AppleServiceImpl ();
@Test
public void creditCheck() {
appleService.creditCheck(request, response);
}
}
CodePudding user response:
You probably want to add a constructor to AppleServiceImpl that takes AppleHelper as a parameter and autowire on the constructor.
Next in your test you want to create an @Mock AppleHelper appleHelper field that you inject using @InjectMocks
