Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jinlii opened this issue Apr 27, 2021 · 1 comment
Open

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

jinlii opened this issue Apr 27, 2021 · 1 comment

Comments

@jinlii
Copy link

jinlii commented Apr 27, 2021

public class fflib_MyList implements IList {
List 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();
}

}

@stohn777
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants