3

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.

  1. Is there a way to configure the class/test relationship?
  2. Does anyone have positive/negative results from implementing option A or B?
  3. Is there a better option C?

2 Answers 2

2

I can see this is going to get closed because it is opinion-based but let me offer the following

  • A vendor tool, like Gearset (which is what I use), can detect via graph analysis all of the test classes that need to run if class X is being deployed. This is extremely handy and fits a nice middle ground between explicitly enumerating test classes to run and running all tests on every deploy
  • Past experience running into issues like yours has taught me to use Option B - every Apex class has a test class. This facilitates a unit-testing approach, made easier by adopting the Platform Enterprise Architecture patterns - notably selector, domain service, and unit of work - that enable mocking.
1
  • Thank you for your response cropredy. It sounds like my first and second questions were answered by your first bullet. Our deployment team uses Gearset. I do not and I did not suggest that they allow Gearset to detect the test classes. I just gave them the class to deploy. It was my mistake to not check the code coverage with just the updated test class. I'll leave this open for a bit to see if anyone else wants to comment. Oct 24, 2023 at 19:36
3

I've heard of a standard where test classes are prefixed with "Test" or "Test_". I abhor that as they all sort together. I always use Test as a suffix of a class name.

On the two proposed methods, I tend to go hybrid. Often for utility classes I'll write a specific test for classes. But, if there are a group of functional classes that work together, I may not write a specific test class for each class but rather write a test class for the feature.

The thing I like about putting Test at the end of the name is if a number of classes implement a feature, they all sort together by the feature name like.

MyFeatureController MyFeatureQueueable MyFeatureBatchable MyFeatureHelper MyFeatureTest

Now, that doesn't mean I might not have more specific test classes also like.

MyFeatureBatchableTest

1
  • Thank you for your thoughts Ken. That seems like a reasonable approach. I agree with your naming scheme. Names are so important when you have 100's of classes, lazy loading, and no list search. Oct 30, 2023 at 19:21

You must log in to answer this question.

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