Home > Enterprise >  Unit Test for function that will call another function with dynamic values
Unit Test for function that will call another function with dynamic values

Time:12-06

I am trying to write a test for a function, rollDays and I got a NullPointerException.

I am not exactly sure how should I deal with the test if the method I want to test will call another function Closed and will take various dynamic values(date) because of the for loop for (int numOfDays = Math.abs(days); Closed(date, mar) || numOfDays-- > 0; date = date.plusDays((long) dir))

The method I want to test is in a class called CalculateDateService: the place where it is called is:
finalDate = rollDays(3, localDate, DateService.marID);

The method inside CalculateDateService:

LocalDate rollDays(int days, LocalDate date, String mar) {
    int dir = days > 0 ? 1 : -1;
    for (int numOfDays = Math.abs(days); Closed(date, mar) || numOfDays-- > 0; date = date.plusDays((long) dir)){
    }
    return date
}
Boolean Closed(LocalDate date, String mar) {
    return dateService.Holiday(date, mar) || dateService.Disruption(date, mar) || this.NonSettle(date);
}
Boolean NonSettle(LocalDate date) {
    LocalDate dayBeforeChristmas = LocalDate.of(dateService.today().getYear(), 12, 24);
    LocalDate dayBeforenewYear = LocalDate.of(dateService.today().getYear(), 12, 31);
    return (date.isEqual(dayBeforeChristmas) || date.isEqual(dayBeforenewYear));
}

In the test:

@Mock
private DateService dateService;

@InjectMocks
private CalculateDateService tested;

@Test
public void testRollDays() {
        LocalDate initDate = LocalDate.of(2022, 12, 22);
       
        //i suspect I have something wrong below 
        when(tested.Closed(initDate, dateService.marID)).thenReturn(Boolean.FALSE);
        when(dateService.today()).thenReturn(initDate);
        when(tested.NonSettle(initDate)).thenReturn(Boolean.FALSE);
        LocalDate rolledDate = tested.rollDays(4, initDate, DateService.marID);
        LocalDate correctRolledDate = LocalDate.of(2022, 12, 29);
        assertEquals(correctRolledDate, rolledDate);
}

The error I got is:

java.lang.NullPointerException at CalculateDateService.NonSettle
at CalculateDateService.Closed at CalculateDateServiceTest.testRollDays

Any help will be greatly appreciated. Thanks in advance.

CodePudding user response:

The Exception is thrown because tested is neither a Mock or a Spy.

You might want to try something along the lines of this:

    @Test
    void testSpyFalseRoll1201by24(){
        var year = LocalDate.now().getYear();
        var spy = Mockito.spy(new CalculateDateService());
        Mockito.doReturn(false).when(spy).nonSettle(Mockito.any(LocalDate.class));
        var result = spy.rollDays(24, LocalDate.of(year, 12, 1), "");
        Assert.assertEquals(result, LocalDate.of(year, 12, 25));
    }

    @Test
    void testSpyTrueOn1224Roll1201by24(){
        var year = LocalDate.now().getYear();
        var spy = Mockito.spy(new CalculateDateService());
        Mockito.doReturn(false).when(spy).nonSettle(Mockito.any(LocalDate.class));
        Mockito.doReturn(true).when(spy).nonSettle(LocalDate.of(year, 12, 24));
        var result = spy.rollDays(24, LocalDate.of(year, 12, 1), "");
        Assert.assertEquals(result, LocalDate.of(year, 12, 26));
    }

What I have done here is stubbing. This works fine in your case but you need to be careful:

Real spies should be used carefully and occasionally, for example when dealing with legacy code.

Mockito documentation "Spying on real objects".

And below this, the documentation says:

Mockito does not delegate calls to the passed real instance, instead it actually creates a copy of it.

Note that I removed DateService, just because it is not part of the problem. Here is my simplified definition of CalculateDateService:

public class CalculateDateService {
    LocalDate rollDays(int days, LocalDate date, String mar) {
        int dir = days > 0 ? 1 : -1;
        for(int numOfDays = Math.abs(days); closed(date, mar) || numOfDays-- > 0; date = date.plusDays((long) dir)) {

        }
        return date;
    }

    Boolean closed(LocalDate date, String mar){
        return this.nonSettle(date);
    }

    Boolean nonSettle(LocalDate date)
    {
        int year = LocalDate.now().getYear();
        LocalDate dayBeforeChristmas = LocalDate.of(year, 12, 24);
        LocalDate dayBeforenewYear = LocalDate.of(year, 12, 31);
        return (date.isEqual(dayBeforeChristmas) || date.isEqual(dayBeforenewYear));
    }
}
  • Related