fflib-apex-mocks icon indicating copy to clipboard operation
fflib-apex-mocks copied to clipboard

fflib_MyList has 0 coverage when I run MockTest. Is that normal?

Open jinlii opened this issue 3 years ago • 1 comments

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

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

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();
}

}

@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();
}

}

jinlii avatar Apr 27 '21 18:04 jinlii

@jinlii That is the expected behavior.

By creating mockList in the test, a virtual copy of fflib_MyList is created that is used instead. Generally mocking classes, as done in this test, is for isolating the target of testing from referenced dependencies. In this case, 'mockListwould be provided to a class being tested so that the business-logic version offflib_MyList` is not used.

stohn777 avatar May 18 '21 19:05 stohn777