Вы находитесь на странице: 1из 11

6/15/2014 Example: Write a test class for our deduping trigger!

Salesforce coding lessons for the 99%


http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 1/11
SALESFORCE CODING LESSONS FOR THE 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
Example: Write a test class for our
deduping trigger!
N O V E M B E R 2 , 2 0 1 3
Preface This post is part of the Write Your First Intermediate Trigger series.
Lets write a test class for our deduping trigger, using Apex testing best practices!
Heres a look at the test class well do a line-by-line breakdown immediately afterwards:
@isTest
public class TestFindDupes {
static testMethod void testDupes() {
// Let's create our records from scratch!
Contact c = new Contact();
c.FirstName = 'Stephen';
c.LastName = 'Curry';
c.Email = 'stephcurry@gsw.com';
insert c;

// Now let's create a dupe lead
Lead dupeLead = new Lead();
dupeLead.FirstName = 'Steph';
dupeLead.LastName = 'Curry';
dupeLead.Company = 'Golden State Warriors';
dupeLead.Email = 'stephcurry@gsw.com';

// This is a special way of doing "risky" things
try {
insert dupeLead;
} catch (Exception e) {
System.debug('An error happened, as predicted!');
}
BEGINNER TUTORIALS LOGIN TO SFDC99 HOW TO READ CODE ABOUT ME Follow @dvdkliu

for post updates!
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 2/11

// Now we try to find our dupe lead, by email
List<Lead> dupes = [SELECT Id FROM Lead
WHERE Email = 'stephcurry@gsw.com'];
System.assertEquals(0, dupes.size());

// Now we "break" our trigger by inserting a non-dupe
Lead legitLead = new Lead();
legitLead.FirstName = 'David';
legitLead.LastName = 'Lee';
legitLead.Company = 'Golden State Warriors';
legitLead.Email = 'dlee@gsw.com';
insert legitLead;
// Now we try to find our legit lead, by email
List<Lead> legits = [SELECT Id FROM Lead
WHERE Email = 'dlee@gsw.com'];
System.assertEquals(1, legits.size());
}
}
Dont be intimidated theres a decent amount of code but none of it is complicated!
Lets do a line by line analysis:
@isTest
public class TestFindDupes {
static testMethod void testDupes() {
...
}
}
The above is the usual fluff that you dont need to worry about.
// Let's create our records from scratch!
Contact c = new Contact();
c.FirstName = 'Stephen';
c.LastName = 'Curry';
c.Email = 'stephcurry@gsw.com';
insert c;
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 3/11
Were creating a new contact from scratch (Testing Best Practice #1!) that will serve as the original
record to dedupe against. Remember that we use the email address to know if its a dupe or not! There
can never be another Steph Curry!
// Now let's create a dupe lead
Lead dupeLead = new Lead();
dupeLead.FirstName = 'Steph';
dupeLead.LastName = 'Curry';
dupeLead.Company = 'Golden State Warriors';
dupeLead.Email = 'stephcurry@gsw.com';
Were creating a dupe lead (by email address) remember that nothing happens until we insert it!
// This is a special way of doing "risky" things
try {
insert dupeLead;
} catch (Exception e) {
System.debug('An error happened, as predicted!');
}
This technique is known as a try/catch statement. It lets you try to do something that may break your
code (like divide by zero, or in our case, insert a dupe). If an error is caught, it doesnt break your code
and lets you move on.
Without try/catch, our code would break here! Well cover this and System.debug soon!
// Now we try to find our dupe lead, by email
List<Lead> dupes = [SELECT Id FROM Lead
WHERE Email = 'stephcurry@gsw.com'];
System.assertEquals(0, dupes.size());
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 4/11
Now that we attempted to create the dupe lead, we try to find it using SOQL. Test classes are run in a
clean version of your org all records are wiped! So the only records that exist are the ones we
created in this test.
We use System.assertEquals() to check that no leads with the duplicate email are found. We check this
by making sure there are exactly 0 leads that are returned by the SOQL query. If a lead is found, it
means that our trigger isnt working as expected, and System.assertEquals() will error out the test.
Using System.assertEquals() is Testing Best Practice #2!
// Now we "break" our trigger by inserting a non-dupe
Lead legitLead = new Lead();
legitLead.FirstName = 'David';
legitLead.LastName = 'Lee';
legitLead.Company = 'Golden State Warriors';
legitLead.Email = 'dlee@gsw.com';
insert legitLead;
// Now we try to find our legit lead, by email
List<Lead> legits = [SELECT Id FROM Lead
WHERE Email = 'dlee@gsw.com'];
System.assertEquals(1, legits.size());
Were creating a lead that isnt a dupe now and checking that our trigger doesnt incorrectly catch it! By
testing scenarios that our trigger shouldnt work on, were fulling Testing Best Practice #3!
Login to Sfdc99 to see this test class in action!
Very proud of you for making it through the first four chapters of Sfdc99! Were ready to start moving
on to some more advanced techniques!
Very soon now youll be a full-fledged Salesforce developer!
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 5/11
newsfdcdev
MARCH 30, 2014 @ 11: 24 PM
Is there a good place to learn more about thefluff, i.e. this part static testMethod void testDupes() {
What specifically does it mean?
Reply
Next post: Introduction to Governor Limits!
12 Comments
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 6/11
Davi d Li u
MARCH 31, 2014 @ 6: 02 PM
This book will help you with that side of things:
http://www.sfdc99.com/2013/05/20/the-best-way-to-quickly-learn-how-to-code-in-
salesforce/
You can also Google the terms and official Apex documentation will explain in more
detail =)
I chose not to explain those in the earlier chapters (but I will in later ones) because
the topics are too advanced for the scope of work in the beginning. Plus, other
places explain them in better detail!
Repl y
Amit
MARCH 17, 2014 @ 5: 09 AM
Hi David,
Thanks for detail information to write test class with nice images to remember. :-)
I have below question regarding above test class:
In test class, we are inserting lead as below:
// This is a special way of doing risky things
try {
insert dupeLead;
} catch (Exception e) {
System.debug(An error happened, as predicted!);
}
// Now we try to find our dupe lead, by email
List dupes = [SELECT Id FROM Lead
WHERE Email = 'stephcurry@gsw.com'];
System.assertEquals(0, dupes.size());
and then querying Lead which has email id as stephcurry@gsw.com. So, Wont it give one record in
List?
If it will give one record then wont it fail assertEqual() here?
Please solve my confusion over here.
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 7/11
Amit
MARCH 28, 2014 @ 3: 11 AM
Thank you very much Skall and David! :-)
Reply
Davi d Li u
MARCH 17, 2014 @ 7: 46 PM
SKall nailed it you re 100% right!
Thank you SKall!
David
Repl y
SKallakuri
MARCH 17, 2014 @ 2: 52 PM
Hello Amit,
When you first tried to insert the dupeLead, it would give you an error since a contact with the
same email already exists. Hence the lead would not be created. So the size of the list would be
0.
Hope this helps.
SKall
Reply
Thank you,
Amit
Reply
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 8/11
Davi d Li u
NOVEMBER 23, 2013 @ 8: 27 AM
Hey Neha,
Sometimes when writing test classes, you need to create a lot of records to fully test
your code! In these cases, it s common to run into Governor Limits:
http://www.sfdc99.com/2013/11/17/what-are-governor-limits/
Test.startTest() resets your Governor Limits it s only available while testing but it is
very helpful!! I will have a post on this in the future!
David
Repl y
neha
NOVEMBER 21, 2013 @ 3: 55 AM
I came across methods like Test.start test() and Test .stoptest()in Test classes. what do they mean and
where exacty in code we need to use it? In few examples its written above the DML statements and in
other case, Its been covered with entire code. Please clarify this.
Reply
neha
NOVEMBER 21, 2013 @ 3: 49 AM
I am just learning to write testclasses.
This i have tried for theMAP TRAINING class in ur org.kindly validate it.
Public class TestMapTraining{
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 9/11
neha
NOVEMBER 21, 2013 @ 4: 40 AM
Or I should write in this way??
Public class TestMapTraining{
static test method void map(){
Account a= new Account();
a.name=012;
a.email=neha.gg@gmail.com;
insert a;
Map m= new Map();
m.put(a.id, a);
MapTraining mt= new MapTraining();
mt.updateaccounts(m);
String name=mt.lst.get(id); -hard coded id
system.assertequals(1234,name);
}
}
Reply
static test method void map(){
Account a= new Account();
a.name=012;
a.email=neha.gg@gmail.com;
insert a;
MapTraining mt= new MapTraining();
a.name=1234;
update a;
mt.updateaccounts(a.id,a.name);
String name=mt.lst.get(id); -hard coded id
system.assertequals(1234,name);
}
}
Reply
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 10/11
neha
NOVEMBER 24, 2013 @ 9: 49 PM
Yes, Thank u
My mistakes.
1) forgot @ISTEST
2) i have used reserved keyword map as test method name
Davi d Li u
NOVEMBER 23, 2013 @ 8: 24 AM
Hey Neha,
I made a few modifications try this out instead! Comments are in line:
@IsTest
public class TestMapTraining{
static testmethod void maptrainingtest(){
// Create an account
Account a = new Account();
a.name = 012;
insert a;
// Create a map
Map accountMap = new Map();
accountMap.put(a.Id, a);
// Test the MapTraining
MapTraining mt= new MapTraining();
mt.updateaccounts(accountMap);
// Check if the name of the account was updated correctly
Account updatedAccount = [SELECT Id, Name FROM Account WHERE Id =
:a.Id];
system.assertequals( 1234, updatedAccount.Name);
}
}
David
Repl y
6/15/2014 Example: Write a test class for our deduping trigger! Salesforce coding lessons for the 99%
http://www.sfdc99.com/2013/11/02/example-write-a-test-class-for-our-deduping-trigger/ 11/11
3)need to practice more on queries too..
Thanks
neha
Reply
Leave a Reply
Enter your comment here...
THEME: SI MPLE STYLE BY FI MPLY

Вам также может понравиться