vendredi 25 décembre 2015

I am geting NullpointerException when I invoke method using java reflection

I am geting NullpointerException when I invoke method using java reflection
can somebody please help

JUNIT TEST CASE

I am trying to test method
private boolean isHoliday(DateCalculationParams dateCalculationParams,Date date) in Class DateCalculationService and i want to mock holidayDlService.isHoliday(holiday)

package colruyt.calendarejb.service.bl;

import static org.junit.Assert.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.mockito.Mockito.*;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import colruyt.calendarejb.bo.DateCalculationParams;
import colruyt.calendarejb.bo.Holiday;
import colruyt.calendarejb.bo.LogicalCalendar;
import colruyt.calendarejb.exception.CountryNotSupportedException;
import colruyt.calendarejb.exception.InvalidCombinationException;
import colruyt.calendarejb.exception.InvalidDateCalculationParamsException;
import colruyt.calendarejb.exception.InvalidHolidayException;
import colruyt.calendarejb.exception.InvalidLogicalCalendarException;
import colruyt.calendarejb.service.dl.HolidayDlService;
import colruyt.calendarejb.util.LogicalCalendarStartdayCts;
import colruyt.calendarejb.util.LogicalCalendarTypeCts;

public class DateCalculationServiceTest {

DateCalculationService dcs;

@Before
public void setUp() throws Exception {
dcs = spy(new DateCalculationService());

}


@Test
public final void isHolidayTest() throws InvalidCombinationException,         CountryNotSupportedException,
        InvalidHolidayException, NoSuchMethodException, SecurityException,        IllegalAccessException,
        IllegalArgumentException, InvocationTargetException {

    DateCalculationParams dcp = new DateCalculationParams();
    dcp.setCountry("IN");
    Date d = new Date();
    Holiday holiday = new Holiday();
    holiday.setDate(d);
    Boolean b = true;
    HolidayDlService holidayDlService = mock(HolidayDlService.class);
    when(holidayDlService.isHoliday(holiday)).thenReturn(true);
    // doReturn(true).when(hs).isHoliday(holiday);
    Method privateStringMethod =                                  DateCalculationService.class.getDeclaredMethod("isHoliday",
            DateCalculationParams.class, Date.class);

    privateStringMethod.setAccessible(true);
//I am getting nullpointer exception at this line
    Boolean s = (Boolean) privateStringMethod.invoke(dcs, dcp, d);

    assertTrue(b);

}

}

Following is the class and method I am testing Class Under Test

Class DateCalculationService{

private boolean isHoliday(DateCalculationParams dateCalculationParams,

    Date date) {

    Holiday holiday = new Holiday();
    holiday.setDate(date);
    boolean isHoliday;
    if (dateCalculationParams.getCountry() != null) {
        holiday.setCountryCode(dateCalculationParams.getCountry());
    }
    try {

   isHoliday = holidayDlService.isHoliday(holiday);

    } catch (InvalidHolidayException e) {

    } 
return isHoliday;
}
}

Aucun commentaire:

Enregistrer un commentaire