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....!!!!
Good work
ReplyDeleteThis 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
very nice.....
ReplyDeleteinternship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai
Very Nice...
ReplyDeleteinternship in chennai for ece students with stipend
internship for mechanical engineering students in chennai
inplant training in chennai
free internship in pune for computer engineering students
internship in chennai for mca
iot internships
internships for cse students in
implant training in chennai
internship for aeronautical engineering students in bangalore
inplant training certificate
Very Nice...
ReplyDeleteinternship in chennai for ece students with stipend
internship for mechanical engineering students in chennai
inplant training in chennai
free internship in pune for computer engineering students
internship in chennai for mca
iot internships
internships for cse students in
implant training in chennai
internship for aeronautical engineering students in bangalore
inplant training certificate
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
Thanks for the lovely blog. It helped me a lot. I'm glad I found this blog. Thanks for sharing with us, I too am always learning something new from your post.
ReplyDelete360DigiTMG Data Science Courses
I can configure my new idea from this post. Detailed information is given. Thank you all for this valuable information...
ReplyDelete360DigiTMG Data Science Certification
Writing in style and getting good compliments on the article is hard enough, to be honest, but you did it so calmly and with such a great feeling and got the job done. This item is owned with style and I give it a nice compliment. Better!
ReplyDeleteData Analytics Course in Bangalore
Nice Information Your first-class knowledge of this great job can become a suitable foundation for these people. I did some research on the subject and found that almost everyone will agree with your blog.
ReplyDeleteCyber Security Course in Bangalore
Writing in style and getting good compliments on the article is hard enough, to be honest, but you did it so calmly and with such a great feeling and got the job done. This item is owned with style and I give it a nice compliment. Better!
ReplyDeleteCyber Security Training in Bangalore
Really nice and interesting blog information shared was valuable and enjoyed reading this one. Keep posting. Thanks for sharing.
ReplyDeleteData Science Training in Hyderabad
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
Great article with excellent information found resourceful and enjoyed reading it thank you, looking forward for next blog.
ReplyDeletetypeerror nonetype object is not subscriptable
I have to search sites with relevant information ,This is a
ReplyDeletewonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
Data Science Training in Bangalore
Such a great blog, thanks for sharing the blog.
ReplyDeleteielts preparation tips
how to become an ethical hacker
stress interview questions
how much java is required for selenium
ethical hacking interview questions
hacking books
I have to search sites with relevant information ,This is a
ReplyDeletewonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
Data Science 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
It's like you understand the topic well, but forgot to include your readers. Maybe you should think about it from several angles.
ReplyDeleteBusiness Analytics Course
I recently came across your article and read it. I want to express my admiration for your writing skills and your ability to get readers to read from start to finish. I would love to read more recent articles and share my thoughts with you.
ReplyDeleteBest Data Science Courses in Bangalore
I was basically inspecting through the web filtering for certain data and ran over your blog. I am flabbergasted by the data that you have on this blog. It shows how well you welcome this subject. Bookmarked this page, will return for extra. data science course in jaipur
ReplyDeleteInformative blog
ReplyDeletebest digital marketing institute in hyderabad
Informative blog
ReplyDeletedata science training in jamshedpur
Informative blog
ReplyDeletedata science training in ludhiana
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
ReplyDelete