Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Am trying to write a Junit Test case for one of the class. But getting error while trying to do it,
Test class looks like below -
public class IntegratorClassTest{
@InjectMocks
IntegratorClass integratorClass;
@Mock
RequestClass requestClass;
@Mock
ContentList contentResponse;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
@Test
public void getCmsOffersTest()throws Exception{
ContentService contentService = Mockito.mock(ContentService.class);
RequestClass requestClass = Mockito.mock(RequestClass.class);
ContentList contentResponse = getContentList();
when(contentService.getContentCollection()).thenReturn(contentResponse);
Validator validator = Mockito.mock(Validator.class);
List<OfferDetails> offerList = new ArrayList<OfferDetails>();
Mockito.doNothing().when(validator).validateData(offerList);
List<OfferDetails> offerListResult = integratorClass.getCmsOffers(contentService, requestClass);
assertTrue(offerListResult.size()>0);
And the implementation class looks something like below -
public class IntegratorClass {
private static final Logger LOGGER = LoggerFactory.getLogger(IntegratorClass.class);
@Autowired
Validator validator;
public List<OfferDetails> getCmsOffers(ContentService contentService,RequestClass requestClass)throws Exception{
LOGGER.info("Entered method getCmsOffers to get the list of offers from CMS");
List<OfferDetails> offerList = new ArrayList<OfferDetails>();
ContentList contentResponse = null;
contentResponse = contentService.getContentCollection();
offerList = getOfferListFromCmsResponse(contentResponse, requestClass);
LOGGER.info("Total number of active offers we got from CMS are -" + offerList.size());
}catch (Exception e)
ErrorResponse errorResponse = PromotionalOffersUtilities.createErrorResponse("500", e.getMessage(),"Getting error while fetching content from CMS - getCmsOffers", ErrorResponse.Type.ERROR);
LOGGER.error("Getting error while fetching content from CMS with Error Message: " + e.getMessage());
throw new ServiceException(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR);
//failing here
validator.validateData(offerList);
LOGGER.info("Exiting method getCmsOffers");
return offerList;
When I ran it in debug mode am error for the line validator.validateData(offerList);
.
It is returning "NullPointerException
".
–
You need to include the Validator
when mocking dependencies so that it will be injected into the subject under test
@Mock
Validator validator;
Also when arranging the behavior of the validator
use an argument matcher for the invoked member as the current setup wont match because they will be comparing different instances when exercising the test.
Mockito.doNothing().when(validator).validateData(any(List<OfferDetails>.class));
You are manually mocking the other dependencies within the test method so they are not needed out side of that
The test now becomes
public class IntegratorClassTest{
@InjectMocks
IntegratorClass integratorClass;
@Mock
Validator validator;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
@Test
public void getCmsOffersTest()throws Exception{
//Arrange
ContentService contentService = Mockito.mock(ContentService.class);
RequestClass requestClass = Mockito.mock(RequestClass.class);
ContentList contentResponse = getContentList();
Mockito.when(contentService.getContentCollection()).thenReturn(contentResponse);
Mockito.doNothing().when(validator).validateData(any(List<OfferDetails>.class));
//Act
List<OfferDetails> offerListResult = integratorClass.getCmsOffers(contentService, requestClass);
//Assert
assertTrue(offerListResult.size() > 0);
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.