
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.
88 lines
2.4 KiB
Plaintext
88 lines
2.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
By default tests will run in system mode, i.e. without taking into account users permissions. In order to be realistic, a test needs to run Business logic code in User context. This is done by encapsulating the tested code in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm[``++System.runAs()++``].
|
|
|
|
|
|
This rule raises an issue when a test function, i.e. a function annotated with ``++@isTest++`` or ``++testMethod++``, does not contain a ``++System.runAs()++`` call.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,apex]
|
|
----
|
|
@isTest
|
|
private class TestClass {
|
|
@isTest
|
|
public static void testMethod() { // NonCompliant
|
|
// Setup test data
|
|
User u = new User(...);
|
|
Case c = new Case (Name = 'Test');
|
|
Test.startTest();
|
|
Insert c;
|
|
Test.stopTest();
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,apex]
|
|
----
|
|
@isTest
|
|
private class TestClass {
|
|
@isTest
|
|
public static void testMethod() {
|
|
// Setup test data
|
|
User u = new User(...);
|
|
Case c = new Case (Name = 'Test');
|
|
System.runAs(u) {
|
|
Test.startTest();
|
|
Insert c;
|
|
Test.stopTest();
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== 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 ``++System.runAs()++``. This indicates that the test code has been factorized and the rule would raise false positives.
|
|
|
|
|
|
== Resources
|
|
|
|
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm[Using the runAs Method]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Enclose your test code in "System.runAs()"
|
|
|
|
|
|
=== Highlighting
|
|
|
|
The test method signature
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 5 Jul 2019, 17:30:42 Nicolas Harraudeau wrote:
|
|
If we raise too many false positives a possible exception is:
|
|
|
|
|
|
=== 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 ``++System.runAs()++``. This indicates that the test code has been factorized and the rule would raise false positives.
|
|
|
|
endif::env-github,rspecator-view[]
|