The Principles of Apex Testing
The Principles of Apex Testing
Agenda:
StartTest()
StopTest()
Assertions
*Principle #2, Use @isTest And
@TestSetup
*Principle #3, Use Asserts
A test without Assert methods isn’t a test, it’s a
liability. Three Assert methods built-in
• System.Assert(boolean-expression, ‘friendly
message’)
• System.AssertEquals(expect, actual, ‘friendly
message’)
•System.AssertNotEquals(expected, actual, ‘friendly message)
Every test method should include at least one assertion.
Good test methods include more than one.
The most robust, and helpful test methods include two sets of
asserts.
*Principle #3, Use Asserts
Top Tester Top Tip: You can write your own assert methods!
@isTest
public class customAssertions{
public class customAssertionException extends exception{}
public Boolean AssertDuplicateAccount(Account a, Account b){
// … compare accounts
if(duplicate != true) {
Throw new customAssertionException(‘whoa, it’s not the
same’);
}
return true; // Return true, without exception when
assertion is true.
}
}
*Principle #4, use startTest and
Start (then) Stop, Collaborate and Listen stopTest
Test.startTest() and Test.stopTest() help facilitate testing.
Isolates your code from the proper setup of the test(s)
startTest() resets DML, CPU Time and other governor limits, ensuring any limits you hit come
from your tested code!
stopTest() forces asynchronous code to complete.
All of the code before this method should be used to initialize variables, populate data structures, and so
on, allowing you to set up everything you need to run your test. Any code that executes after the call to
startTest and before stopTest is assigned a new set of governor limits.
*Principle #5 How to
test: Positive Tests Given
Valid
Input
Execute
d
I believe you can do it! Code
User w/ No Access
Perm before Perm
Set Set applied
User
w/o
No Acces
Perm
Set
*Principle #6, User Tests
c;
}
}
*Principle #6, Positive User Tests
private class exampleCode_Tests {
@isTest static void test_getBankAccount_Positive()
{ exampleCode drWho = new exampleCode();
User u =
AwesomeTestLib.getUserWithProfile(‘DoctorProfile’
);
Account a = (Account)TestFactory.createSObject(new Account());
Integer result;
System.runAs(u)
{ Test.startTest
();
result =
drWho.getBankAc
count(a);
Test.stopTest();
*Principle #6, Negative User Tests
private class exampleCode_Tests {
@isTest static void test_getBankAccount_UberForNope()
{ exampleCode Dalek = new exampleCode();
User u =
AwesomeTestLib.getUserWithProfile(‘DalekProfile’);
Account a = (Account)TestFactory.createSObject(new Account());
Integer result;
System.runAs(u)
{ Test.startTest
();
result =
Dalek.getBankAc
count(a);
Test.stopTest();
}
*Principle #7, Testing with Perm Sets
private class exampleCode_Tests {
@isTest static void test_getBankAccount_W_PermSet()
{ exampleCode ClaraOswald= new exampleCode();
User u = AwesomeTestLib.getUserWithProfile(‘Standard
User’);
UtilityClass.AssignUserToPermissionSet(u, ‘Companion’);
Account a = (Account)TestFactory.createSObject(new Account());
Boolean result;
System.runAs(u){
Test.startTest();
result = ClaraOswald.canAccessTardis(a);
Test.stopTest();
}
System.assertNotEquals(result, null,
‘Expected ClaraOswald who has Companion Permissions to have access to the
Tardis’);
*Principle #8, Use your own data
Because, someone will delete the account named “do not delete, used for testing”
Always build your own test data.
If you created the data, you can precisely assert against it.
Unless you have to, never use @isTest(seeAllData=true)
List of Acceptable Reasons to use @seeAllData = true.
*Principle #8, Use your own data
Found in:
http://bit.ly/1c5exnV
• With it you can say:
• Account a = (Account)TestFactory.createSObject(new Account());
• Opportunity o = (Opportunity)TestFactory.createSObject(new
Opportunity(AccountId = a.Id));
• Account[] aList = (Account[])TestFactory.createSObjectList(new
Account(), 200);
*Principle #8, Use your own data
Examples:
*Principle #8, Use your own data
Examples:
*Principle #8, Use your own data
Loading Test Data
Using the Test.loadData method, you can populate data in your test methods without having to
write many lines of code.
Follow these steps:
-Add the data in a .csv file.
-Create a static resource for this file.
-Call Test.loadData within your test method and passing it the sObject type token and the
static resource name.
For example, for Account records and a static resource name of myResource, make the
following call:
Calls a service
Returns ???
*Principle #9, Mocking - Testing a single
HTTP callout
*Principle #9, Mocking Testing multiple
HTTP callouts
*Principle #9, Mocking Testing multiple
HTTP callouts
*Principle #10, What is Test.isRunningTest()method
The Test.isRunningTest() method is used to identify, if the piece of code being executed is invoked
from a Test class execution or from other artefacts such as a Trigger, Batch Job etc. Returns true if
the code being executed is invoked from a test class otherwise returns a false. Example:
1. To ensure the trigger doesn’t execute the batch if Test.IsRunningTest() is true, and then test the
batch class with it’s own test method.
2. Testing callouts – in our callout code we check to see if we‘re executing within a unit test context
by checking Test.isRunningTest() and instead of getting your callout response from an
HttpResponse.send() request, you return a pre-built test string instead.
If you have any questions, just call me