Here is have provided some of the basics to write perfect test class in salesforce
Here I'll explain with short snippets for the test classes
As the first one below
(seeAllData=false)
You can set it true or false on your own, there are two condition for setting it true or false
1. If you are querying any object and fetching records from the database in the test class then set it to true as well there are few object we can't create in our test like "OpportunityLineItem" so to test with this object records, we need to query it with "seeAllData = true".
2. But if you are inserting test records of an object an then querying it in the test class, then you need to set it false otherwise test class won't be executed.
TESTING CUSTOM CONTROLLER AND STANDARD CONTROLLERr
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
How to cover custom controller in the test class?
If you need to cover constructor in the test class then simply define it in the test class, like CountContactOfAccount is the name of the class, just define it in the test class as in below, it will cover you whole constructor.
How to call StandardController (ApexPages.StandardController stdController)?
If you are using standard controller in the class then you need to define it in the test class too
TESTING WITH STATIC AND VOID METHODS
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
How to call methods in the test class?
As you have seen above we have called controller , by using controller we can call static and void methods easily
So if the method is void like "public void something() {"
But if the method is static like "public static list something() { "
then you can directly call the method no need to use deifned controller
TESTING ApexPages.currentPage().getParameters().get('ID')
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
In this scenario you just need to put the value in the test of the variable that you fetching from the the URL
ASSERTS (Most Important thing for test classes)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
There as three most commonly used asserts are there shown below, use asserts to check that your method are returning the correct values or not.
TESTING ERROR MESSAGES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
If you have error message in your test class and you want to cover it in test class then use this snippet
I think these are the most common scenarios for the Test classes that makes some mess.
Some key points:
1. Whole orgs average code coverage should be 75%, NOT each class.
2. Each trigger must more then 0% code coverage.
Hope I've helped you
CHEERS....!!!!
Here I'll explain with short snippets for the test classes
As the first one below
1: @isTest(seeAllData=false)
2: private class Test_CountContactOfAccount {
3: //Method
4: static testMethod void CountContactOfAccountUnitTest() {
5: }
6: }
(seeAllData=false)
You can set it true or false on your own, there are two condition for setting it true or false
1. If you are querying any object and fetching records from the database in the test class then set it to true as well there are few object we can't create in our test like "OpportunityLineItem" so to test with this object records, we need to query it with "seeAllData = true".
2. But if you are inserting test records of an object an then querying it in the test class, then you need to set it false otherwise test class won't be executed.
TESTING CUSTOM CONTROLLER AND STANDARD CONTROLLERr
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
How to cover custom controller in the test class?
If you need to cover constructor in the test class then simply define it in the test class, like CountContactOfAccount is the name of the class, just define it in the test class as in below, it will cover you whole constructor.
1: //Calling contructor
2: CountContactOfAccount controller = new CountContactOfAccount();
How to call StandardController (ApexPages.StandardController stdController)?
If you are using standard controller in the class then you need to define it in the test class too
1: //Insert you object that you are using in your class
2: Account acc = new Account(Name = 'test');
3: insert acc;
4: //Define standard controller and pass inserted object
5: ApexPages.StandardController stdController = new ApexPages.StandardController stdController(acc);
6: //Now define your controller then pass standard controller you've just defined
7: CountContactOfAccount controller = new CountContactOfAccount(stdController );
TESTING WITH STATIC AND VOID METHODS
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
How to call methods in the test class?
As you have seen above we have called controller , by using controller we can call static and void methods easily
So if the method is void like "public void something() {"
1: //Calling contructor
2: CountContactOfAccount controller = new CountContactOfAccount();
3: //Calling void method
4: controller.something();
But if the method is static like "public static list
then you can directly call the method no need to use deifned controller
1: //Calling static method
2: CountContactOfAccount.something(listOfAccount);
TESTING ApexPages.currentPage().getParameters().get('ID')
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
In this scenario you just need to put the value in the test of the variable that you fetching from the the URL
1: //this method will put Id in the URL in the test class
2: currentPageReference().getParameters().put('id', '001c384348247811');
ASSERTS (Most Important thing for test classes)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
There as three most commonly used asserts are there shown below, use asserts to check that your method are returning the correct values or not.
1: System.assert(pBoolean)
2: System.assertEquals(pANY, pANY)
3: System.assertNotEquals(pANY, pANY)
TESTING ERROR MESSAGES
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
If you have error message in your test class and you want to cover it in test class then use this snippet
1: //List of Message
2: List msgs = ApexPages.getMessages();
3: //Define a boolean variable
4: boolean msg = false;
5: //Loop through the messages of the page
6: for(Apexpages.Message msg:msgs){
7: //if there is error message then set the boolean to true
8: if (msg.getDetail().contains(‘Search requires more characters’)) msg = true;
9: }
10: //Assert
11: system.assert(msg);
12: //Single Assert on Error Messages
13: System.assert(ApexPages.getMessages()[0].getDetail().contains( 'User not found with given username' ));
I think these are the most common scenarios for the Test classes that makes some mess.
Some key points:
1. Whole orgs average code coverage should be 75%, NOT each class.
2. Each trigger must more then 0% code coverage.
Hope I've helped you
CHEERS....!!!!
This test really help for fighting exam of salesforce developer because without aware about the basics no one get the actual certificate of salesforce developer.
ReplyDeleteThis is really good for learners...:)
ReplyDeleteHi Abhi..
ReplyDeleteHow to Write TestClass For Trigger can you explain me?
Hi Ashok,
DeleteIn the Test class for trigger, you dont have to call the Controller, as there isn't any, most of the things explained above are same as in test class for triggers, you just need to create a scenario as you have taken in the Trigger, as trigger runs automatically according to the conditions, and then check for the results using asserts.
Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
ReplyDeleteSalesforce Training
nice
ReplyDeleteThis post really helpful to the beginner who wish to shine in this field keep updates...
ReplyDeleteSalesforce training in Chennai
Thanks John
ReplyDeleteHi Ashok,
ReplyDeleteIn the Test class for trigger, you dont have to call the Controller, as there isn't any, most of the things explained above are same as in test class for triggers, you just need to create a scenario as you have taken in the Trigger, as trigger runs automatically according to the conditions, and then check for the results using asserts.
Thanks Rishi
ReplyDeleteThanks Jaya
ReplyDeleteThanks Vemula
ReplyDeletevery interesting class for sales person,thanks for sharing.
ReplyDeleteiPhone app developers India
android app development companies
Mobile game development companies India
mobile app development companies
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeleteMicrosoft Windows Azure Training | Online Course | Certification in chennai | Microsoft Windows Azure Training | Online Course | Certification in bangalore | Microsoft Windows Azure Training | Online Course | Certification in hyderabad | Microsoft Windows Azure Training | Online Course | Certification in pune
You actually make it seem like it's really easy with your acting, but I think it's something I think I would never understand. I find that too complicated and extremely broad. I look forward to your next message. I'll try to figure it out!
ReplyDeleteArtificial Intelligence Course in Bangalore
You actually make it seem like it's really easy with your acting, but I think it's something I think I would never understand. I find that too complicated and extremely broad. I look forward to your next message. I'll try to figure it out!
ReplyDeleteArtificial Intelligence Course in Bangalore
DxMinds Innovation Labs is one of the leading iso certified https://dxminds.com/mobile-game-development-company-in-india/
ReplyDeleteWe have an expert team of developers for creating game applications on various platforms at affordable prices and a 100% success rate. Get in touch with us at info@dxminds.com
I will definitely digg it and personally suggest to my friends. I am confident they will be benefited from this website. Body building Product Review
ReplyDeleteI love your blog! You always have such interesting and thought-provoking content. google workspace account
ReplyDelete