 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website, studysalesforce.com, a video library to learn Salesforce. So in this video, I'm going to demonstrate how we can use Adverite Test Setup annotation so that we can create test setup methods in EPIX test classes. So first, let's discuss some theoretical points and then I will be implementing test setup method. So use test setup methods, methods that are annotated with Adverite Test Setup to create test records once and then access them in every test method in the test class. So what you need to do, you need to create a setup method annotated with Adverite Test Setup annotation. And whatever data you create in that setup method, your EPIX test class, each method can access those data. So test setup methods can be time saving when you need to create reference or prerequisite data for all test methods or a common set of records that all test methods operate on. If a test class contains a test setup method, the testing framework executes the test setup method first before any test method in the class. Records that are created in the test setup methods are available to all test methods in the test class and are rolled back at the end of test class execution. This is important point like if a test method changes those records, such as record feed updates or record deletions, those changes are rolled back after each test method finishes execution. The next executing test method gets access to the original unmodified state of those records, right? So this I'm going to demo you as well. So in my previous demonstration, in previous video, I demoed you like how we can create test classes while implementing triggers. So I will be implementing a test setup method here as well. But let's understand that with another demo and then I will be converting this. So test setup EPIX. So this will be a dummy class. So we need to use at the rate test setup annotation. So this way you can create a method and then I'm going to create count list of account. Now I'm going to implement a loop. So I'm going to create two account records. So account ACC equals to new account ACC dot name equals to test plus I am going to add this into ACC list. And after this loop, I'm writing insert ACC list. So this way I created this setup method. Now, whatever method I will be creating after this, so I can query these records and I can apply any process. And remember one thing these records won't be available in the org, right? So there is some error. So be to check that. Okay. So I need to use at the rate is test annotation before the class that I forgot. So now this error will be removed automatically. Right? So this you need to remember before class, you need to write at the rate is test. Then for setup method, you need to write at the rate setup. And now I'm going to write one more method. So public static void method one. So now here I'm going to query the record. So account ACC one equals to select ID name on account where. So I'm going to check name, where name equals to test zero. So it will be queried and will be available into ACC one. So I'm writing ACC one dot phone equals to one, two, three, four, five, six. Now I'm doing update operation on ACC one. Right. Then account ACC two equals two. So this time I'm going to query test one. And I'm going to write delete ACC two. Right. So in this method one, I varied both the account reports that I created in setup method. I updated one and I deleted another one. So in the presentation, I told you whatever we do in this method that won't be happening in another method. So let's test it. So private static void method two. Now in this method, I'm going to apply assertion. So I'm going to query this here as well. So if this phone is updated, so updated phone will be available, but initially phone is null. So what I'm going to do, I'm going to write assert equal. So expected null and ACC one dot phone. So if ACC one dot phone is null, so it will be equals to this. It means whatever modification we did here, that is not affecting the actual account report that we created that is limited to this method on the right. So let's save this and run test. Okay. So here also I need to write at this test. Because if we don't write this, so these won't be converting into test methods. So I'm opening this as well. Running test. So here you can see two methods executed and they both passed. It means whatever changes we are doing in method one, like we updated phone account one, if your name is test zero, but in method two, it is still none. It means whatever modification we did here, it is not available here. So this way, if you create some data here, so that will be unique for each method. You can query that method. Sorry, you can query that data and you can do some changes or whatever operation you want. And in another method, original state will be queried. Right. So this way you can create bulk data in this setup method and then in individual methods, you can query that data. So now in this code, like which we implemented in previous demonstrations. So I'm just going to give you a quick revision. So here in this trigger, I'm going to call copy billing to shipping or insert an update. This is the code. If this chat box values true, then all billing information will be copied into shipping information. And for that, I created this test method for insert. I created this bulk data and then for update also, I created this bulk data and applied the code. So you can refer the previous video to understand this. Now I'm going to convert this into setup method. So I need to write these codes here. So I'm just pasting it here. Now inside this, I need to create this data. So I'm copying and pasting it here. And here I'm writing one if condition if I double equals to zero, then only this will be true. Otherwise falls. So for one record, this chat box will be true and for one, it will be false so that insert and update both can be performed. So I'm removing these from here. So this is our setup method. Now setup method is inserting account record. So insert part will be covered from this automatically. So I am removing this code. Right. So what I did, I created this annotation at the rate test setup. Then I created setup method where I created two account records and both are having billing information. One that is like a first account record is having built copy billing address to shipping address. This checkbox is true. And second account will be having it as false. So for first account record that trigger will be executed automatically because it is true. But for second, it won't be working. So here I don't need to create data. So you can see in in previous state like both for both insert and update, I created separate data. But now you can see data is created once and I will be querying it here. So what I will do account ACC equals to select ID, comma, this feed layer equals to test one because we provided this test plus I. So for second account record name will be test one. And here I'm removing this. So I need to use this ACC. I'm removing this loop. We need to update the account. So this way I varied this record in ACC. Then ACC dot this field is updated to true. Then update this account. So it is throwing some error. So I'm writing limit one as well. Right. So updated account I'm querying through ACC dot ID. And here as well ACC dot billing city and updated account dot shipping city. So this way this code is updated. So here you can see I have not created any data. Data is created in setup and I have updated this code. So I varied this here. Right. I'm saving this code. So here you can see it is right now not covered. It is also not covered. So now I am going to run this test. So it is showing some error. Let me check this. Okay. So we didn't query this and we check it here. So billing city also I need to query. So if we are using any field, so we need to query that so that we can have its value. So again, I'm going to run test. So this time you can see it is passing. If I come here, so here you can see it is completely covered through this. So 100% code coverage and for trigger also code coverage is 100%. You can see both insert and update are working. So this way I hope with this simple demonstration you understood how to use at the rate test setup annotation to create setup method or bulk data and you can query that data in separate test methods. And in real-time scenario also I implemented this setup method using at the rate test setup annotation. And here I query that data. So the benefit of this, you don't need to create data in separate methods. You just need to create data once and you can query that in separate methods. And if you do some changes, so those changes won't be available in another method. So another method will be having original state of that data. So this is the best part of this setup method. So whenever you want to write test classes so that you can check your Apex code and you can provide the code coverage. So this is the best practice. So I hope you understood whatever I demonstrated in this video. If you want to watch more Salesforce related videos, so you can visit studysalesforce.com or video library to learn Salesforce. Thank you for watching this video.