ApexMocks String Matching Methods

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

String Matchers

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

stringIsBlank()

Matches on null, empty strings, or white space.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyMap mockMap = (MyMap)mocks.mock(MyMap.class);

mocks.startStubbing();
    mocks.when(mockMap.get(fflib_Match.anyString()))
        .thenReturn('DEFAULT');
    mocks.when(mockMap.get(fflib_Match.stringIsBlank()))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockMap.get(null));
System.assertEquals('MATCH', mockMap.get(''));
System.assertEquals('MATCH', mockMap.get('      '));
System.assertEquals('DEFAULT', mockMap.get('ApexMocks'));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyList mockList = (MyList)mocks.mock(MyList.class);

// Test
mockList.add(null);
mockList.add('');
mockList.add('      ');

// Passing verify statement
((IList)mocks.verify(mockList, 3))
    .add(fflib_Match.stringIsBlank());

stringIsNotBlank()

Matches on any non-blank string.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyMap mockMap = (MyMap)mocks.mock(MyMap.class);

mocks.startStubbing();
    mocks.when(mockMap.get(fflib_Match.anyString()))
        .thenReturn('DEFAULT');
    mocks.when(mockMap.get(fflib_Match.stringIsNotBlank()))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('DEFAULT', mockMap.get(null));
System.assertEquals('DEFAULT', mockMap.get(''));
System.assertEquals('DEFAULT', mockMap.get('      '));
System.assertEquals('MATCH', mockMap.get('ApexMocks'));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyList mockList = (MyList)mocks.mock(MyList.class);

// Test
mockList.add('ApexMocks');

// Passing verify statement
((IList)mocks.verify(mockList))
    .add(fflib_Match.stringIsNotBlank());

stringStartsWith(prefix)

Matches on any string that starts with the specified prefix (case-sensitive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyMap mockMap = (MyMap)mocks.mock(MyMap.class);

mocks.startStubbing();
    mocks.when(mockMap.get(fflib_Match.anyString()))
        .thenReturn('DEFAULT');
    mocks.when(mockMap.get(fflib_Match.stringStartsWith('EMEA')))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockMap.get('EMEA-12345'));
System.assertEquals('DEFAULT', mockMap.get('USA-67890'));
System.assertEquals('MATCH', mockMap.get('EMEA-8888'));

// stringStartsWith() matching is case-sensitive
System.assertEquals('DEFAULT', mockMap.get('emea-8888'));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyList mockList = (MyList)mocks.mock(MyList.class);

// Test
mockList.add('EMEA-12345');
mockList.add('USA-67890');
mockList.add('EMEA-8888');

// Passing verify statement
((IList)mocks.verify(mockList, 2))
    .add(fflib_Match.stringStartsWith('EMEA'));

// stringStartsWith() matching is case-sensitive
((IList)mocks.verify(mockList, fflib_ApexMocks.NEVER))
    .add(fflib_Match.stringStartsWith('emea'));

stringEndsWith(suffix)

Matches on any string that ends with the specified suffix (case-sensitive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyMap mockMap = (MyMap)mocks.mock(MyMap.class);

mocks.startStubbing();
    mocks.when(mockMap.get(fflib_Match.anyString()))
        .thenReturn('DEFAULT');
    mocks.when(mockMap.get(fflib_Match.stringEndsWith('.pdf')))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockMap.get('AnnualReport.pdf'));
System.assertEquals('DEFAULT', mockMap.get('MeetingNotes.docx'));
System.assertEquals('MATCH', mockMap.get('CompanyAccount.pdf'));

// stringEndsWith() matching is case-sensitive
System.assertEquals('DEFAULT', mockMap.get('AnnualReport.PDF'));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyList mockList = (MyList)mocks.mock(MyList.class);

// Test
mockList.add('AnnualReport.pdf');
mockList.add('MeetingNotes.docx');
mockList.add('CompanyAccounts.pdf');

// Passing verify statement
((IList)mocks.verify(mockList, 2))
    .add(fflib_Match.stringEndsWith('.pdf'));

// stringEndsWith() matching is case-sensitive
((IList)mocks.verify(mockList, fflib_ApexMocks.NEVER))
    .add(fflib_Match.stringEndsWith('.PDF'));

stringContains(substring)

Matches on any string that contains the specified substring (case-sensitive).

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyMap mockMap = (MyMap)mocks.mock(MyMap.class);

mocks.startStubbing();
    mocks.when(mockMap.get(fflib_Match.anyString()))
        .thenReturn('DEFAULT');
    mocks.when(mockMap.get(fflib_Match.stringContains('Marketing')))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockMap.get('Sales;Marketing;Design'));
System.assertEquals('DEFAULT', mockMap.get('Sales;Design;Engineering'));
System.assertEquals('MATCH', mockMap.get('Engineering;Design;Marketing'));

// stringContains() matching is case-sensitive
System.assertEquals('DEFAULT', mockMap.get('sales;marketing;design'));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyList mockList = (MyList)mocks.mock(MyList.class);

// Test
mockList.add('Sales;Marketing;Design');
mockList.add('Sales;Design;Engineering');
mockList.add('Engineering;Design;Marketing');

// Passing verify statement
((IList)mocks.verify(mockList, 2))
    .add(fflib_Match.stringContains('Marketing'));

// stringContains() matching is case-sensitive
((IList)mocks.verify(mockList, fflib_ApexMocks.NEVER))
    .add(fflib_Match.stringContains('marketing'));

stringMatches(regex)

Matches on strings that match the specified regular expression.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyMap mockMap = (MyMap)mocks.mock(MyMap.class);

mocks.startStubbing();
    mocks.when(mockMap.get(fflib_Match.anyString()))
        .thenReturn('DEFAULT');
    mocks.when(mockMap.get(
        fflib_Match.stringMatches('^[A-Za-z0-9+_.-]+@(.+)$')))
            .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('MATCH', mockMap.get('terry.tester@testing.com'));
System.assertEquals('DEFAULT', mockMap.get('terry.tester'));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyList mockList = (MyList)mocks.mock(MyList.class);

// Test
mockList.add('terry.tester@testing.com');

// Passing verify statement
((IList)mocks.verify(mockList))
    .add(fflib_Match.stringMatches('^[A-Za-z0-9+_.-]+@(.+)$'));

anyString()

Matches on any non-null string.

Stubbing Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyMap mockMap = (MyMap)mocks.mock(MyMap.class);

mocks.startStubbing();
    mocks.when(mockMap.get(fflib_Match.stringIsBlank()))
        .thenReturn('DEFAULT');
    mocks.when(mockMap.get(fflib_Match.anyString()))
        .thenReturn('MATCH');
mocks.stopStubbing();

// Test
System.assertEquals('DEFAULT', mockMap.get(null));
System.assertEquals('MATCH', mockMap.get(''));
System.assertEquals('MATCH', mockMap.get('      '));
System.assertEquals('MATCH', mockMap.get('ApexMocks'));
System.assertEquals('MATCH', mockMap.get('EMEA-12345'));
System.assertEquals('MATCH', mockMap.get('USA-67890'));
System.assertEquals('MATCH', mockMap.get('EMEA-8888'));
System.assertEquals('MATCH', mockMap.get('AnnualReport.pdf'));
System.assertEquals('MATCH', mockMap.get('MeetingNotes.docx'));
System.assertEquals('MATCH', mockMap.get('CompanyAccount.pdf'));
System.assertEquals('MATCH', mockMap.get('Sales;Marketing;Design'));
System.assertEquals('MATCH', mockMap.get('Sales;Design;Engineering'));
System.assertEquals('MATCH', mockMap.get('Engineering;Design;Marketing'));
System.assertEquals('MATCH', mockMap.get('terry.tester@testing.com'));

Verify Example

// Setup
fflib_ApexMocks mocks = new fflib_ApexMocks();
MyList mockList = (MyList)mocks.mock(MyList.class);

// Test
mockList.add('');
mockList.add('      ');
mockList.add('ApexMocks');
mockList.add('EMEA-12345');
mockList.add('USA-67890');
mockList.add('EMEA-8888');
mockList.add('AnnualReport.pdf');
mockList.add('MeetingNotes.docx');
mockList.add('CompanyAccounts.pdf');
mockList.add('Sales;Marketing;Design');
mockList.add('Sales;Design;Engineering');
mockList.add('Engineering;Design;Marketing');
mockList.add('terry.tester@testing.com');

// Passing verify statement
((IList)mocks.verify(mockList, 13))
    .add(fflib_Match.anyString());