I am new to the Junit. Please suggest what is going wrong in the below code -:
Mockito.when(daoimpl.getEcgs(request)).thenReturn(listOfecg);
getEcgs() takes the DashboardRequest
object and the POJO if this class is as -:
public String eligibilityCarrierGroupId;
public Set<String> carrierIds;
@LastModifiedDate
public Date fromDate;
@LastModifiedDate
public Date toDate;
and the listOfecg POJO is
private String ecgId;
private List<String> carrier;
Now, consider i am setting up each and every field with some values and even after that i am getting the null pointer exceptions on
Mockito.when(daoimpl.getEcgs(request)).thenReturn(listOfecg);
Please advice me what might be going wrong.
Full Code-:
public class DashboardDataServiceTest {
//Set<String> testSet = new HashSet<String>();
private MockMvc mockMvc;
@Mock
private DashboardService dashboardService;
@InjectMocks
private DashboardDataController dashboardController;
@InjectMocks
DashboardDaoImpl daoimpl= new DashboardDaoImpl();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(dashboardController).build();
}
@Test
public void testDashboardInfo() throws Exception {
DashboardRequest request = new DashboardRequest();
DashboardDataResponse response = new DashboardDataResponse();
List<String> carrierIds = new ArrayList<>();
Set<String> testSet = new HashSet<String>();
carrierIds.add("KCY");
List<DashboardStatusCount> overall = new ArrayList<>();
DashboardStatusCount dashstatus = new DashboardStatusCount();
dashstatus.setStatus("Success");
dashstatus.setCount(12);
overall.add(dashstatus);
request.setEligibilityCarrierGroupId("aaa");
testSet.add("KCY");
request.setCarrierIds(testSet);
request.setFromDate(new Date());
request.setToDate(new Date());
response.setOverallTotals(271);
response.setOverall(overall);
List<EligibilityCarrierGroupingDoc> listOfecg = new ArrayList<>();
List<String> carrierIdsx = new ArrayList<>();
EligibilityCarrierGroupingDoc ecgDoc = new EligibilityCarrierGroupingDoc();
ecgDoc.setEcgId("aaa");
carrierIdsx.add("KCY");
ecgDoc.setCarrier(carrierIdsx);
listOfecg.add(ecgDoc);
Mockito.when(daoimpl.getEcgs(request)).thenReturn(listOfecg);
DashboardDataResponse responsedata = dashboardService.getDashBoardResponseData(request);
System.out.println("The response data is "+responsedata);
}
private String mapToJson(Object object) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(object);
}
}
Thanks in advance.