Im trying to write the junit for the below code and kind of stuck.
public List<PartGEntity> getBEntityId(List<GBEntityDTO> gBEntities, BigInteger partId) {
List<PartGEntity> partGEntityList = new ArrayList<>();
if(!CollectionUtils.isEmpty(gBEntities)) {
gBEntities.forEach(gBEntityDTO -> {
PartGEntity partGEntity = createPartGEntity(
statClient.getBEntityByAnetId(gBEntityDTO.getAnetId()));
partGEntity.setPartyId(partyId);
partGEntityList.add(partGEntity);
});
}
return partGEntityList;
}
I have written the below junit for this one.But its failing.
BEntityDTO bEntityByAnetId = new BEntityDTO();
when(statClient.getBEntityByAnetId("5738")).thenReturn(bEntityByAnetId);
when(partGEntityDS.getBEntityId(newBEntityList,partId)).thenReturn(partGEntityList);
Im mocking the statClient as above. But when executing the testcases,Im getting a null pointer at the below line
createPartGEntity(statClient.getBEntityByAnetId(gBEntityDTO.getAnetId()));
While executing the statClient. I did a debug here as well. Tried to evaluate each of the expression. while evaluating the expression, gBEntityDTO.getAnetId(), Im getting proper result. But while trying to evaluate this entire line (statClient.getBEntityByAnetId(gBEntityDTO.getAnetId())), getting a null pointer exception. Not able to resolve this. As I have done the mocking above. But still not able to understand why the null pointer.
Any help on this is much appreciated.
Thanks in advance