0

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.

codeLover
  • 2,571
  • 1
  • 11
  • 27
Rishabh Bansal
  • 362
  • 3
  • 7
  • 15
  • How are you creating your daoimpl mock? (impl is an odd name to include in a mock object). – Clive Evans Oct 18 '18 at 10:57
  • Post your full code(test code part). Maybe your dao is null – drowny Oct 18 '18 at 10:57
  • Did you de bugged and checked weather internally it is your getEcgs() method is calling other service. If so you need to Mock that service as well. – John Oct 18 '18 at 10:57
  • Please see the full code in the recent edit and advice. Thanks for your replies. – Rishabh Bansal Oct 18 '18 at 11:01
  • Can you past your exception logs and code of the method that your are covering. – John Oct 18 '18 at 11:11
  • @John i am not getting any logs for the same just the line number and the code is as follows -: public List getEcgs(DashboardRequest dashboardRequest) { List ecgData = mongoTemplate.findAll(EligibilityCarrierGroupingDoc.class); return ecgData; – Rishabh Bansal Oct 18 '18 at 11:18
  • You should try replacing .getEcgs(request) with .getEcgs(any()) – Jeppz Oct 18 '18 at 11:18
  • @jeppz its not working with Mockito.any(Classname.class). – Rishabh Bansal Oct 18 '18 at 11:24
  • 3
    **@Mock** is used to define the mock object, while **@InjectMock** annotates the real object you want to test. I think you have just done the opposite... – Benoit Oct 18 '18 at 11:48
  • @John this is correct .Thanks for the reply. – Rishabh Bansal Oct 18 '18 at 11:52
  • Why are you creating a new instance of `DashboardDaoImpl`? You have to mock it instead. – Jobin Oct 19 '18 at 00:49
  • even though the question is closed , this might help you to find the answer. use @RunWith(MockitoJUnitRunner.class) in the class if you use Junit, if you use TestNG find the right runner. – hunter Oct 19 '18 at 05:14

0 Answers0