rspec/rules/S5394/apex/rule.adoc

110 lines
3.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
In order to protect shared resources, Salesforce enforces a maximum number of SOQL queries, DML queries, ``++@future++`` method, etc... execution inside a single https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_transaction.htm[transaction]. This is part of https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm[Governor limits].
Every test method should check how close the tested code is to reach the governor limits. This is done by calling ``++Test.StartTest()++`` before the tested code and ``++Test.StopTest()++`` after it.
This rule raises an issue when a test method, i.e. a method annotated with ``++@isTest++`` or ``++testmethod++``, does not contain a call to ``++Test.StartTest()++`` and ``++Test.StopTest()++``.
=== 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
----
@isTest()
public class MyTestClass {
@isTest()
static void myTestMethod() { // Noncompliant
// Preparing test (creating records, etc...)
// test code with no reference to Test.StartTest() or Test.StopTest()
}
static testmethod void myTestMethod2() { // Noncompliant
// Preparing test (creating records, etc...)
// test code with no reference to Test.StartTest() or Test.StopTest()
}
static void myHelper() {
// ...
}
}
----
=== 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()
public class MyTestClass {
@isTest()
static void myTestMethod() {
// Preparing test (creating records, etc...)
Test.StartTest();
// test code with no reference to Test.StartTest() or Test.StopTest()
Test.StopTest();
}
static testmethod void myTestMethod2() {
// Preparing test (creating records, etc...)
Test.StartTest();
// test code with no reference to Test.StartTest() or Test.StopTest()
Test.StopTest();
}
static void myHelper() {
// ...
}
}
----
=== Exceptions
2021-04-28 16:49:39 +02:00
No issue will be raised if the test class, i.e. the class annotated with ``++@isTest++``, contains helper methods, i.e. methods not annotated with ``++@isTest++`` or ``++testmethod++``, which contain calls to ``++Test.StartTest()++`` or ``++Test.StopTest()++``. This indicates that the test code has been factorised and the rule would raise false positives.
== Resources
2021-04-28 16:49:39 +02:00
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm[Execution Governors and Limits]
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm[Using Limits, startTest, and stopTest]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add "Test.StartTest()" and "Test.StopTest()" to your test
=== Highlighting
The test method signature
'''
== Comments And Links
(visible only on this page)
=== on 5 Jul 2019, 17:28:50 Nicolas Harraudeau wrote:
If we raise false positives a possible exception would be:
=== Exceptions
No issue will be raised if the test class, i.e. the class annotated with ``++@isTest++``, contains helper methods, i.e. methods not annotated with ``++@isTest++`` or ``++testmethod++``, which contain calls to ``++Test.StartTest()++`` or ``++Test.StopTest()++``. This indicates that the test code has been factorised and the rule would raise false positives.
=== on 8 Aug 2019, 09:32:02 Quentin Jaquier wrote:
SONARSLANG-397 shows that, even with the exception above, we have a lot of issue, with potential false positives.
We decided to keep the rule, but remove it from the default quality profile.
endif::env-github,rspecator-view[]