5

Newbie coder here, so I could use some help with test coverage.

I've got a class for creating test/training data in a sandbox. At the beginning of that class, I've got a couple of utility methods to get the dates I'll need. They're meant to be relative to when the class is run. So the method giveMonthInFY figures out what today is, then gives back the first of the month for when we would be make programs that are in the current fiscal year. (We're an education nonprofit with a July to June fiscal year.) So if I spin up a new training sandbox today, in January, I want to create a Fall program for LAST September (9/1/2018). But if I spin one up in July, I would be creating a program for NEXT September (9/1/2019).

To ensure test coverage, however, I need to actually test handing that method dates before and after July 1. I have no idea how to do that since the method actually calls Today(). Help?

Here's my relevant method to the question:

    //Takes the desired month (as an integer), gives the first of that month in the current FY
public static date giveMonthInFY(integer monthInt) {
    date dateToReturn;
    if (System.today().Month() >= 7) {
        dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1);
    }
    else {
        dateToReturn = date.newinstance(Date.Today().Year(), monthInt, 1)-365;
    }
    return dateToReturn;
}

And here is the test method:

    @isTest
public static void testgiveMonthInFY(){
    date testDate3 = Date.newinstance(2018, 01,01);
    date sept1 = StarWarsTestDataUtil.giveMonthInFY(9);
    system.assertEquals(Date.newinstance(2017, 09, 01), sept1);
    date testDate4 = Date.newinstance(2018, 07,01);
    date sept1FY19 = StarWarsTestDataUtil.giveMonthInFY(9);
    system.assertEquals(Date.newinstance(2018, 09, 01), sept1FY19);
}

The test, when run, only hits the portion of the year we are actually in: image showing code coverage

5

You could refactor this into a new method that allows you to provide a reference date, rather than always relying on the today() context in all cases.

// this single parameter method assumes current date
public static date giveMonthInFY(integer monthInt) {

    // call the 2 param method with today as the reference date
    return giveMonthInFY(monthInt, Date.Today());
}

// this 2 param method allows for injection of a specific date
public static date giveMonthInFY(integer monthInt, date referenceDate) {
    date dateToReturn;
    if (referenceDate.Month() >= 7) {
        dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1);
    }
    else {
        dateToReturn = date.newinstance(referenceDate.Year(), monthInt, 1)-365;
    }
    return dateToReturn;
}
  • I was afraid there wouldn't be any better/easier way. Makes sense though. So off I go to refactor.... – Michael Kolodner Jan 22 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.