It’s been a while I have blogged. For past couple of months, I was on a transition of moving to a new role in a new organization. Now that I finally found some time, I thought of taking this opportunity to share something. Also, I got charged again after hearing that Feedspot, an online blog and website reader, selected my blog among the Top 50 Salesforce Developer Blogs on the planet!!! What good reason one must need to continue blogging? So here I AM BACK!!!
Do you know the biggest challenge I have ever faced during my initial days of developers? No, it was not how to write complex code. The biggest nightmare of mine was writing test classes!!!! Yes, I have spent day and night to ensure that my code is covered properly so that I can move them to production without any hassle. And most of the time I failed to do so.
Initially, I thought its a common problem with every developer. But then I realized that the problem was with me. I was not following the best practices to ensure that I write the most optimal test class for my apex classes. After spending time reading an endless number of blogs and documents, I finally mastered the skill to write a perfect test class.
Now the most important factor of writing a test class is to prepare the test data, which will help you to test scenarios for which your apex class is written. Recently, I found that my team here was struggling with the same, and hence I thought of taking this opportunity to share some of the key points that we need to keep in mind while preparing test data.
This will be shared through a series of blogs, starting with how to prepare test data and use them.
I am not going to talk about what’s a test class, assuming that everybody knows about it. For people who are new to the test class, this would be a good start.
So, here we go!!!
As mentioned earlier, test data are created to test scenarios for which the apex class is written. Of course, you can use data from your org for testing by enabling SeeAllData to true, but that might not work when you deploy your code to an org without any data. Hence always ensure you prepare test data for your test class.
Now there are enormous ways to create and maintain test data. But the best way of doing it would be to write a Test Utility Class.
Test Utility Class
Test Utility Class will consist of reusable methods to create test data. It’s advisable to maintain one test utility class in your org so that developers can reuse existing test methods or add new methods to create test data. Normally, utility classes will be of type public, annotated with @isTest, and all methods would be of type static.
Test Setup Method
Now, while writing test class, start with a method called setup(), would be annotated with @testSetup. This method will contain all prerequisite test data that we have created for the test class. We can all methods from test utility class here to prepare the same.
The significance of using test setup is to avoid multiple creations of test data if your test class contains more than one test method. Thus it reduces the number of DML calls. Once this method is created, the testing framework will execute your setup method first to prepare the test data, and then run the remaining test method(s). Also, keep in mind that your test class should contain only one test setup method and you must set SeeAllData to false.
Access Test Data in Test Methods
Now that we have our test data ready, you must know how to access them in our test methods. And this is where you will understand the power of writing test setup method. All you need to do is to query the test data what you have created in your test setup method. How simple is that!!!
Now I know people who don’t like too much of reading and prefer to do hands-on. Let’s get into some action on my next blog. Till then, ADIOS!!!!