Skip to content

Instantly share code, notes, and snippets.

@randi274
Created November 23, 2020 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randi274/496254459945c16f28466b504ade1b45 to your computer and use it in GitHub Desktop.
Save randi274/496254459945c16f28466b504ade1b45 to your computer and use it in GitHub Desktop.
Test class to control Singleton Stub
@IsTest
public with sharing class SingletonTest {
@IsTest
public static void testLeapYear() {
//not stubbed out, so this will take the normal path w/o the switch
SingletonController.printDateTime();
}
@IsTest
public static void testNonLeapYear() {
//IS stubbed out, so this WILL take the alternate path
DateTime newNow = DateTime.newInstance(2019, 9, 8);
DateTimeProvider.instance = new DateTimeStub(newNow);
SingletonController.printDateTime();
//in the wild, return data and make assertions here
}
private with sharing class DateTimeStub extends DateTimeProvider {
public DateTime now;
public DateTimeStub(DateTime now) {
this.now = now;
}
public override DateTime now() {
return now;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment