ApexMocks Date and Datetime Matching Methods

A reference guide for the ApexMocks library's Date and Datetime matching methods. It includes examples for both stubbing responses and verifying behaviour.

Date and Datetime Matchers

The methods are all accessible as static methods of the fflib_Match class.

dateAfter(minDate)

Matches on any Date after the specified minDate (not inclusive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDate()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(fflib_Match.dateAfter(today)))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Not inclusive, so today is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(today));
System.assertEquals('MATCH', mockApi.getStatus(tomorrow));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

// Test
mockApi.getStatus(today);
mockApi.getStatus(tomorrow);

// Passing verify statement
// Not inclusive, so today is not included in the count
((MyApi)mocks.verify(mockApi, 1))
    .getStatus(fflib_Match.dateAfter(today));

dateAfter(minDate, isInclusive)

Matches on any Date after the specified minDate. Inclusivity determined by isInclusive.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDate()))
        .thenReturn('DEFAULT');
    Boolean isInclusive = true;
    mocks.when(mockApi.getStatus(
        fflib_Match.dateAfter(today, isInclusive)))
            .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Is inclusive, so today is a match
System.assertEquals('MATCH', mockApi.getStatus(today));
System.assertEquals('MATCH', mockApi.getStatus(tomorrow));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

// Test
mockApi.getStatus(today);
mockApi.getStatus(tomorrow);

// Passing verify statement
// Is inclusive, so today is included in the count
Boolean isInclusive = true;
((MyApi)mocks.verify(mockApi, 2))
    .getStatus(fflib_Match.dateAfter(today, isInclusive));

dateBefore(maxDate)

Matches on any Date before the specified maxDate (not inclusive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDate()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(fflib_Match.dateBefore(today)))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockApi.getStatus(yesterday));
// Not inclusive, so today is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(today));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();

// Test
mockApi.getStatus(yesterday);
mockApi.getStatus(today);

// Passing verify statement
// Not inclusive, so today is not included in the count
((MyApi)mocks.verify(mockApi, 1))
    .getStatus(fflib_Match.dateBefore(today));

dateBefore(maxDate, isInclusive)

Matches on any Date before the specified maxDate. Inclusivity determined by isInclusive.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDate()))
        .thenReturn('DEFAULT');
    Boolean isInclusive = true;
    mocks.when(mockApi.getStatus(
        fflib_Match.dateBefore(today, isInclusive)))
            .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockApi.getStatus(yesterday));
// Is inclusive, so today is a match
System.assertEquals('MATCH', mockApi.getStatus(today));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();

// Test
mockApi.getStatus(yesterday);
mockApi.getStatus(today);

// Passing verify statement
// Is inclusive, so today is included in the count
Boolean isInclusive = true;
((MyApi)mocks.verify(mockApi, 2))
    .getStatus(fflib_Match.dateBefore(today, true));

dateBetween(minDate, maxDate)

Matches on any Date between the specified minDate and maxDate (not inclusive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDate()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(
        fflib_Match.dateBetween(yesterday, tomorrow)))
            .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Not inclusive, so yesterday is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(yesterday));
System.assertEquals('MATCH', mockApi.getStatus(today));
// Not inclusive, so tomorrow is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(tomorrow));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

// Test
mockApi.getStatus(yesterday);
mockApi.getStatus(today);
mockApi.getStatus(tomorrow);

// Passing verify statement
// Not inclusive, so neither yesterday 
// nor tomorrow are included in the count
((MyApi)mocks.verify(mockApi, 1))
    .getStatus(fflib_Match.dateBetween(yesterday, tomorrow));

dateBetween(minDate, isInclusive, maxDate, isInclusive)

Matches on any Date between the specified minDate and maxDate. Inclusivity specified by isInclusive parameters.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDate()))
        .thenReturn('DEFAULT');
    Boolean isInclusive = true;
    mocks.when(mockApi.getStatus(
        fflib_Match.dateBetween(
            yesterday, isInclusive,
            tomorrow, isInclusive
        )))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Is inclusive, so yesterday is a match
System.assertEquals('MATCH', mockApi.getStatus(yesterday));
System.assertEquals('MATCH', mockApi.getStatus(today));
// Is inclusive, so tomorrow is a match
System.assertEquals('MATCH', mockApi.getStatus(tomorrow));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date yesterday = Date.today().addDays(-1);
Date today = Date.today();
Date tomorrow = Date.today().addDays(1);

// Test
mockApi.getStatus(yesterday);
mockApi.getStatus(today);
mockApi.getStatus(tomorrow);

// Passing verify statement
// Is inclusive, so both yesterday 
// and tomorrow are included in the count
Boolean isInclusive = true;
((MyApi)mocks.verify(mockApi, 3))
    .getStatus(fflib_Match.dateBetween(
        yesterday, isInclusive,
        tomorrow, isInclusive
    ));

anyDate()

Matches on any non-null Date.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date nullDate = null;
Date thePast = Date.today().addYears(-100);
Date today = Date.today();
Date theFuture = Date.today().addYears(100);

mocks.startStubbing();
    mocks.when(mockApi.getStatus((Date)fflib_Match.isNull()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(fflib_Match.anyDate()))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Matches on any non-null date
System.assertEquals('DEFAULT', mockApi.getStatus(nullDate));
System.assertEquals('MATCH', mockApi.getStatus(thePast));
System.assertEquals('MATCH', mockApi.getStatus(today));
System.assertEquals('MATCH', mockApi.getStatus(theFuture));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Date nullDate = null;
Date thePast = Date.today().addYears(-100);
Date today = Date.today();
Date theFuture = Date.today().addYears(100);

// Test
mockApi.getStatus(nullDate);
mockApi.getStatus(thePast);
mockApi.getStatus(today);
mockApi.getStatus(theFuture);

// Passing verify statement
// Any non-null date included in the count
((MyApi)mocks.verify(mockApi, 3))
    .getStatus(fflib_Match.anyDate());

datetimeAfter(minDatetime)

Matches on any Datetime after the specified minDatetime (not inclusive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDatetime()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(fflib_Match.datetimeAfter(now)))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Not inclusive, so now is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(now));
System.assertEquals('MATCH', mockApi.getStatus(soon));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

// Test
mockApi.getStatus(now);
mockApi.getStatus(soon);

// Passing verify statement
// Not inclusive, so now is not included in the count
((MyApi)mocks.verify(mockApi, 1))
    .getStatus(fflib_Match.datetimeAfter(now));

datetimeAfter(minDatetime, isInclusive)

Matches on any Datetime after the specified minDatetime. Inclusivity determined by isInclusive.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDatetime()))
        .thenReturn('DEFAULT');
    Boolean isInclusive = true;
    mocks.when(mockApi.getStatus(
        fflib_Match.datetimeAfter(now, isInclusive)))
            .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Is inclusive, so now is a match
System.assertEquals('MATCH', mockApi.getStatus(now));
System.assertEquals('MATCH', mockApi.getStatus(soon));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

// Test
mockApi.getStatus(now);
mockApi.getStatus(soon);

// Passing verify statement
// Is inclusive, so now is included in the count
Boolean isInclusive = true;
((MyApi)mocks.verify(mockApi, 2))
    .getStatus(fflib_Match.datetimeAfter(now, isInclusive));

datetimeBefore(maxDatetime)

Matches on any Datetime before the specified maxDatetime (not inclusive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDatetime()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(fflib_Match.datetimeBefore(now)))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockApi.getStatus(fiveMinutesAgo));
// Not inclusive, so now is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(now));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();

// Test
mockApi.getStatus(fiveMinutesAgo);
mockApi.getStatus(now);

// Passing verify statement
// Not inclusive, so now is not included in the count
((MyApi)mocks.verify(mockApi, 1))
    .getStatus(fflib_Match.datetimeBefore(now));

datetimeBefore(maxDatetime, isInclusive)

Matches on any Datetime before the specified maxDatetime. Inclusivity determined by isInclusive.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDatetime()))
        .thenReturn('DEFAULT');
    Boolean isInclusive = true;
    mocks.when(mockApi.getStatus(
        fflib_Match.datetimeBefore(now, isInclusive)))
            .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockApi.getStatus(fiveMinutesAgo));
// Is inclusive, so now is a match
System.assertEquals('MATCH', mockApi.getStatus(now));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();

// Test
mockApi.getStatus(fiveMinutesAgo);
mockApi.getStatus(now);

// Passing verify statement
// Is inclusive, so now is included in the count
Boolean isInclusive = true;
((MyApi)mocks.verify(mockApi, 2))
    .getStatus(fflib_Match.datetimeBefore(now, isInclusive));

datetimeBetween(minDatetime, maxDatetime)

Matches on any Datetime between the specified minDatetime and maxDatetime (not inclusive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDatetime()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(
        fflib_Match.datetimeBetween(fiveMinutesAgo, soon)))
            .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Not inclusive, so fiveMinutesAgo is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(fiveMinutesAgo));
System.assertEquals('MATCH', mockApi.getStatus(now));
// Not inclusive, so soon is not a match
System.assertEquals('DEFAULT', mockApi.getStatus(soon));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

// Test
mockApi.getStatus(fiveMinutesAgo);
mockApi.getStatus(now);
mockApi.getStatus(soon);

// Passing verify statement
// Not inclusive so neither fiveMinutesAgo,
// nor soon are included in the count
((MyApi)mocks.verify(mockApi, 1))
    .getStatus(fflib_Match.datetimeBetween(fiveMinutesAgo, soon));

datetimeBetween(minDatetime, isInclusive, maxDatetime, isInclusive)

Matches on any Datetime between the specified minDatetime and maxDatetime. Inclusivity determined by isInclusive parameters.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

mocks.startStubbing();
    mocks.when(mockApi.getStatus(fflib_Match.anyDatetime()))
        .thenReturn('DEFAULT');
    Boolean isInclusive = true;
    mocks.when(mockApi.getStatus(
        fflib_Match.datetimeBetween(
            fiveMinutesAgo, isInclusive,
            soon, isInclusive
        )))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Is inclusive, so fiveMinutesAgo is included in the count
System.assertEquals('MATCH', mockApi.getStatus(fiveMinutesAgo));
System.assertEquals('MATCH', mockApi.getStatus(now));
// Is inclusive, so soon is included in the count
System.assertEquals('MATCH', mockApi.getStatus(soon));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime fiveMinutesAgo = Datetime.now().addMinutes(-5);
Datetime now = Datetime.now();
Datetime soon = Datetime.now().addMinutes(5);

// Test
mockApi.getStatus(fiveMinutesAgo);
mockApi.getStatus(now);
mockApi.getStatus(soon);

// Passing verify statement
// Is inclusive, so fiveMinutesAgo and soon are
// included in the count
Boolean isInclusive = true;
((MyApi)mocks.verify(mockApi, 3))
    .getStatus(
        fflib_Match.datetimeBetween(
            fiveMinutesAgo, isInclusive,
            soon, isInclusive
        ));

anyDatetime()

Matches on any non-null Datetime.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime nullDatetime = null;
Datetime thePast = Datetime.now().addYears(-100);
Datetime now = Datetime.now();
Datetime theFuture = Datetime.now().addYears(100);

mocks.startStubbing();
    mocks.when(mockApi.getStatus((Datetime)fflib_Match.isNull()))
        .thenReturn('DEFAULT');
    mocks.when(mockApi.getStatus(fflib_Match.anyDatetime()))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
// Any non-null Datetime is a match
System.assertEquals('DEFAULT', mockApi.getStatus(nullDatetime));
System.assertEquals('MATCH', mockApi.getStatus(thePast));
System.assertEquals('MATCH', mockApi.getStatus(now));
System.assertEquals('MATCH', mockApi.getStatus(theFuture));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyApi mockApi = (MyApi)mocks.mock(MyApi.class);

Datetime nullDatetime = null;
Datetime thePast = Datetime.now().addYears(-100);
Datetime now = Datetime.now();
Datetime theFuture = Datetime.now().addYears(100);

// Test
mockApi.getStatus(nullDatetime);
mockApi.getStatus(thePast);
mockApi.getStatus(now);
mockApi.getStatus(theFuture);

// Passing verify statement
// Any non-null Datetime is included in the count
((MyApi)mocks.verify(mockApi, 3))
    .getStatus(fflib_Match.anyDatetime());