2

We are using fflib-apex-common in our project, and we implemented several selectors. By default fflib_SObjectSelector checks for Read permission. But now I am wondering what type of tests, and at what level, should check for this? Is it a test class for the controller which uses the selector? Or is it selector's own test class? Or both?

2 Answers 2

3

The selector itself must be tested. Without it, you cannot verify that it will work for all intended use cases. Further, without a test on the selector, each controller using it would need to individually verify the behavior for correctness.

The controller that uses the selector may choose to test the functionality, but if you've covered the selector sufficiently (and I mean covered with assertions, not just code coverage), there should be no need to repeat those tests elsewhere.

In order to make unit tests useful and practical, you should focus on writing the minimum number of tests that can verify all acceptable outcomes (including any failure modes).

2

In addition to @sfdcfox answer let me add an added bonus for having a selector test class:

Once the selector is thoroughly tested, you can them mock the selector in tests of services, domains, and other classes that invoke selectors. This means you can design and run a lot of unit tests without ever having to create test data with DML - especially if you also use the fflib Unit of Work layer/pattern in service classes. This means your unit tests run faster. When you have hundreds of tests, this means CI and other deployments run much faster.

See fflib ApexMocks github library and especially Andrew Fawcett's book Force.com Enterprise Patterns Chapter 12.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .