About a year ago, I developed an Apex project that involved a trigger/handler. It included the trigger, a trigger handler class, several helper classes, and a test class. All went well.
Recently while deploying an enhancement to the trigger handler class, the code coverage failed because the trigger test class was not being deployed. The scenario is actually a little more complicated than that, but basically a test class is not being deployed that handles some code coverage for the class that is deployed. So that test class is not run during the deployment and the code coverage requirement for the deployed class is not met.
I'm looking for conventions to future-proof incremental class changes and associated unit testing. I don't see anything in the Best Practices that addresses the relationship between Apex Classes and Apex Test Classes.
A couple obvious options:
A. Develop a suite of tests that cover the overall feature (in a single test class). This test class should be updated any time any of the covered classes are updated.
B. Develop an Apex test class for each Apex class and trigger (ClassNameTest), and always deploy the associated test class as the Apex class/trigger is deployed. There will likely be some redundancy in the test classes. Unfortunately the test class redundancy might also require multiple test classes to be updated for a given Apex class change.
As a practical matter, the test class(es) associated with an Apex class should probably be updated any time the Apex class is updated.
I'm probably answering my own question here... I started out thinking option B was the way to go, but as I thought about maintenance going forward, option A started to look better. Also option A encourages me to consider the big picture even though I might be making a small incremental change. I use //#region in VS Code to organize class files, which will help navigate the option A test class file.
- Is there a way to configure the class/test relationship?
- Does anyone have positive/negative results from implementing option A or B?
- Is there a better option C?