How Does Test Coverage Work for Enums and Properties in Apex?

Question:
I recently noticed some unusual behavior in Apex test coverage and wanted to confirm whether this has always been the case or if something has changed over time.
Specifically, I observed that:
- A class containing only an
enumdoes not appear in the test coverage report at all. - Properties behave differently depending on whether they are assigned a value, declared with
{get; set;}, or left as simple declarations.
Consider the following class containing only an enum:
public class MyEnums { // Does not appear in test coverage reports
public enum ExampleEnum {
TryMe,
YetAgain
}
}Even when this enum is used in test cases, the class is completely absent from coverage reports, and Salesforce reports zero lines to cover.
Now, consider a different class that contains various types of properties:
public class CoverageExample { // Shows as 1 of 2 lines covered just by instantiation
public String notCountedOrCovered;
public String countedAndCovered = 'A String';
public String countedAndNotCovered {get; set;}
}When this class is instantiated in a test, Salesforce reports 1 out of 2 lines covered. However, the behavior of each property is different:
notCountedOrCovered is not counted in coverage.countedAndCovered is counted and covered because it has an assignment.countedAndNotCovered is counted but remains uncovered unless explicitly accessed.
Answer
Yes, this behavior has always been the case in Apex. The key rule to remember is:Enums, interfaces, abstract methods, class-level declaration statements, class definitions, and debug statements never count for test coverage.
On the other hand:
Getters and setters, non-abstract methods (including their signatures), class-level assignments, and every non-debug executable line within a method always count for coverage.
Explanation of the Code Snippets
1. Why does the enum class not appear in test coverage?
public class MyEnums { // Not counted for coverage
public enum ExampleEnum {
TryMe,
YetAgain
}
}The entire class consists of a class-level declaration statement (the enum). Apex does not consider enum declarations as executable lines of code. Since there are no methods or executable statements in the class, Salesforce does not include it in test coverage calculations
2. Why is the first property ignored in test coverage?
public String notCountedOrCovered;This is a simple class-level declaration with no assignment. Since it does not contain executable code, it does not contribute to test coverage.
3. Why does the property with a getter/setter count but remain uncovered?
public String countedAndNotCovered {get; set;}When a property is declared with {get; set;}, it is no longer just a declaration. Apex treats {get; set;} as implicit method definitions, which require test coverage. If the property is never accessed in a test, it remains counted but uncovered.
4. Why does the assigned property count and get covered?
public String countedAndCovered = 'A String';The use of = makes this an executable statement, meaning it must be covered. Since this assignment runs when the class is instantiated, it is automatically covered in test execution.
Only use {get; set;} when needed, especially in Visualforce controllers.
Be mindful that assignments (=) count as executable lines.
Understand that enums and simple property declarations do not impact test coverage.
Understanding these nuances helps developers write more efficient and less burdensome test coverage.
Kick Start Your Career with Real-Time Project-Based Salesforce Training
Our Salesforce course is meticulously designed to give you a deep understanding of the Salesforce platform, equipping you with the essential skills to excel in the CRM industry. The curriculum includes key modules such as Salesforce Admin, Developer, and AI, integrating theoretical knowledge with hands-on practice. Through real-world projects and interactive exercises, you’ll develop the expertise needed to tackle complex business challenges using Salesforce solutions. Our experienced instructors provide both technical proficiency and industry insights to help you thrive in the Salesforce ecosystem.
In addition to technical training, our Salesforce Training in Singapore offers personalized mentorship, certification guidance, and interview preparation to enhance your career prospects. You’ll gain access to extensive study materials, practical project experience, and dedicated support throughout your journey. By the end of the course, you’ll be fully prepared for certification exams and equipped with real-world problem-solving skills that employers value. Start your Salesforce career today—enroll in a Free Demo now!

