Skip to main content
7 of 10
edited tags
Cuban coffee
  • 1.6k
  • 1
  • 27
  • 57

MocksApex - behaviour verification

I am using the ApexMocks framework and I am having the following error message:

Class.MockTest.testBehavior: line 13, column 1 11:53:56.3 (10709808)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Anyone with experience with the ApexMocks framework that can tell me why the test is failing?

Class to test:

public class fflib_MyList implements IList {
    List<String> myList;

    public fflib_MyList() {
        this(new List<String>());
    }

    private fflib_MyList(List<String> myList) {
        this.myList = myList;
    }

    public void add(String value) {
        myList.add(value);
    }

    public String get(Integer index) {
        return myList.get(index);
    }

    public void clear() {
        myList.clear();
    }

    public Boolean isEmpty() {
        return myList.isEmpty();
    }

    public interface IList {
        void add(String value);
        String get(Integer index);
        void clear();
        Boolean isEmpty();
    }
}

ApexMocks test:

@IsTest
private class MockTest {
    static testMethod void testBehavior() {
        // Given
        fflib_ApexMocks mocks = new fflib_ApexMocks();
        fflib_MyList.IList mockList = (fflib_MyList.IList)mocks.mock(fflib_MyList.class);

        // When
        mockList.add('bob');

        // Then
        ((fflib_MyList.IList) mocks.verify(mockList)).add('bob');
        ((fflib_MyList.IList) mocks.verify(mockList, fflib_ApexMocks.NEVER)).clear();
    }
}

Source : FinancialForce ApexMocks Framework

Cuban coffee
  • 1.6k
  • 1
  • 27
  • 57