rspec/rules/S5531/apex/rule.adoc

62 lines
2.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
class ApexTests {
private static void testHelper(Task task) {
System.assert("expected subject", task.subject, "task has not the expected subject"); // Noncompliant
}
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,apex]
2021-04-28 16:49:39 +02:00
----
@isTest
class ApexTests {
private static void testHelper(Task task) {
System.assert("expected subject", task.subject, "task has not the expected subject");
}
}
----
== Resources
2021-04-28 16:49:39 +02:00
* 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[]