rspec/rules/S5531/apex/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

62 lines
2.0 KiB
Plaintext

== Why is this an issue?
Asserts should only be used in test code. Salesforce provide better ways to handle errors:
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_exceptions.htm[Trigger Exceptions]
* https://developer.salesforce.com/blogs/2017/09/error-handling-best-practices-lightning-apex.html[Error Handling Best Practices for Lightning and Apex]
Classes containing test code, including test helper methods, should always have the ``++@isTest++`` annotation. There is a https://help.salesforce.com/articleView?id=000314162&language=en_US&type=1&mode=1[limit to the amount of code] an organization can upload into Salesforce. Test code does not count towards this limit, and test code is recognized via the ``++@isTest++`` annotation.
This rule raises an issue when methods ``++System.assert++``, ``++System.assertEquals++`` or ``++System.assertNotEquals++`` are called in a class which is not annotated with ``++@isTest++``.
=== Noncompliant code example
[source,apex]
----
class ApexTests {
private static void testHelper(Task task) {
System.assert("expected subject", task.subject, "task has not the expected subject"); // Noncompliant
}
}
----
=== Compliant solution
[source,apex]
----
@isTest
class ApexTests {
private static void testHelper(Task task) {
System.assert("expected subject", task.subject, "task has not the expected subject");
}
}
----
== Resources
* https://help.salesforce.com/articleView?id=000314162&language=en_US&type=1&mode=1[Increase Apex code character limit]
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_utility_classes.htm[Common Test Utility Classes for Test Data Creation]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Either annotate the class with "@isTest" or remove this assert statement.
=== Highlighting
``++System.assert++``, ``++System.assertEquals++`` and ``++System.assertNotEquals++`` statements
endif::env-github,rspecator-view[]