I've realized (despite spending forever making this lol) in this guide I do not specifically cover testing a trigger in Apex. This is because I have become so used to using trigger handler/domain classes over the years that I end up testing the logic in them the exact same way I test a typical apex class! That said, to test an apex trigger, you simply need to do a dml statement that would trigger the trigger. If you have a Case trigger that triggers after update, you need to update a case in your apex test to fire your trigger. If you have a Contact trigger that fires before or after insert you need to insert a new contact in your test class to fire the trigger, etc. This will cause your trigger to fire and will allow you to gain coverage of your trigger. However, if the bulk of your trigger logic is in your triggers, I would strongly suggest moving them to a trigger handler or domain class! It will make your life so much better and easier as a dev! I even have a video covering it :) Additionally in this video when I describe what happens in a @TestSetup I state that the data is deleted... this was a bad attempt at simplifying the process that takes place. What actually happens is the data is rolled back to its original testsetup state at the end of each test method execution. In more complex testsetup situations I have seen, in the pas,t that testsetup rollbacks end up adding to governor limits, which is why I've mentioned it here.
Thank you so much for making this video Matt, I have found it very helpful! Just a quick question, so, if we are using DataFactory, we will use it in the @TestSetup? Or DataFactory methods can be used directly inside of the test methods?
Hello Matt, thank you for all your video, I'm getting to understand Apex ! I'm halfway through the video but the part about testsetup was surprising for me. I worked for an ISV where our devs struggled to make some test because we had a ton of data to create beforehand and I was thinking: "maybe they should have split their test in multiple classes with the same test setup". You already corrected it in your comment but I would like to emphasize that the test setup run only once and only rollback between test if necessary. Here is the paragraph from the Salesforce documentation "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 a test setup method are available to all test methods in the test class and are rolled back at the end of test class execution. If a test method changes those records, such as record field 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."
We can also consider bulkifying the test methods with bulk test data to check if the code can handle large amount of data (real life production scenario) without failing for exceptions (101/501) etc
Heya Matt, this is fantastic thanks man! I still need to go through the whole video but getting testing right is something that we've been struggling with in our team so getting a good idea on best practices will help a lot! I was also wondering, do you have any recommendations on resources where a developer can learn more about AI in Salesforce? Everything I've seen, including the AI associate, seems high level. I would love to learn how to implement custom NLP models and LLM in Salesforce
Very helpful. I just ran the first test, and I noticed that my output table (?) looks different from yours at time 16:00. I don't have the "CLASSES" "PERCENT" "UNCOVERED LINES". Instead I get a summary that has two columns, NAME and VALUE which shows some information (incl Pass Rate 100%). Just curious why my output would be different. Win 10 with latest version of VSCode. Thank you so much for making this video. I've already picked up some useful information & reminders in the 16 minutes I've watched, looking forward to the rest.
Hi @CodingwithTheForce can i Please know the lwc master series which your planning which i saw in one of the vedio could you please let us know when your Planning to upload Eagerly waiting,Thank you!
Is there anything in documentation that outlines that test setup runs per each test method? I tested in a dev edition and I'm only seeing the DML's run once per the entire test run which includes multiple test methods.
I've realized (despite spending forever making this lol) in this guide I do not specifically cover testing a trigger in Apex. This is because I have become so used to using trigger handler/domain classes over the years that I end up testing the logic in them the exact same way I test a typical apex class! That said, to test an apex trigger, you simply need to do a dml statement that would trigger the trigger. If you have a Case trigger that triggers after update, you need to update a case in your apex test to fire your trigger. If you have a Contact trigger that fires before or after insert you need to insert a new contact in your test class to fire the trigger, etc. This will cause your trigger to fire and will allow you to gain coverage of your trigger. However, if the bulk of your trigger logic is in your triggers, I would strongly suggest moving them to a trigger handler or domain class! It will make your life so much better and easier as a dev! I even have a video covering it :)
Additionally in this video when I describe what happens in a @TestSetup I state that the data is deleted... this was a bad attempt at simplifying the process that takes place. What actually happens is the data is rolled back to its original testsetup state at the end of each test method execution. In more complex testsetup situations I have seen, in the pas,t that testsetup rollbacks end up adding to governor limits, which is why I've mentioned it here.
I’m glad I found out your channel ❤ thanks for the videos hope you keep posting and us keep learning from you
Thank you so much for making this video Matt, I have found it very helpful! Just a quick question, so, if we are using DataFactory, we will use it in the @TestSetup? Or DataFactory methods can be used directly inside of the test methods?
Hello Matt, thank you for all your video, I'm getting to understand Apex !
I'm halfway through the video but the part about testsetup was surprising for me. I worked for an ISV where our devs struggled to make some test because we had a ton of data to create beforehand and I was thinking: "maybe they should have split their test in multiple classes with the same test setup".
You already corrected it in your comment but I would like to emphasize that the test setup run only once and only rollback between test if necessary.
Here is the paragraph from the Salesforce documentation "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 a test setup method are available to all test methods in the test class and are rolled back at the end of test class execution. If a test method changes those records, such as record field 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."
This guy is a God among Safeforce dev, I'm so grateful. Thank you so much
This is a real masterclass. Thank you thank you thank you!!
Thanks for all your videos; they really helped me with fflib, and now I understand testing better.
i press like button even before watching your video! i'am starting my journey soon as a salesforce developper in Paris thank you !
We can also consider bulkifying the test methods with bulk test data to check if the code can handle large amount of data (real life production scenario) without failing for exceptions (101/501) etc
Heya Matt, this is fantastic thanks man!
I still need to go through the whole video but getting testing right is something that we've been struggling with in our team so getting a good idea on best practices will help a lot!
I was also wondering, do you have any recommendations on resources where a developer can learn more about AI in Salesforce? Everything I've seen, including the AI associate, seems high level. I would love to learn how to implement custom NLP models and LLM in Salesforce
Very helpful. I just ran the first test, and I noticed that my output table (?) looks different from yours at time 16:00. I don't have the "CLASSES" "PERCENT" "UNCOVERED LINES". Instead I get a summary that has two columns, NAME and VALUE which shows some information (incl Pass Rate 100%). Just curious why my output would be different. Win 10 with latest version of VSCode.
Thank you so much for making this video. I've already picked up some useful information & reminders in the 16 minutes I've watched, looking forward to the rest.
Hi @CodingwithTheForce can i Please know the lwc master series which your planning which i saw in one of the vedio could you please let us know when your Planning to upload Eagerly waiting,Thank you!
I hope to release the entire course in ~2 months
LWC masterclass when?
I hope to release the entire course in ~2 months
great trainign!
Great Video!
Nice, new Video ❤
My man switched to VS Code😀
U are epic
Hi is there a way to do multiple callouts and set different mocks for each one ?
have your mock hold a list of responses and pop each one out as callouts are made
Is there anything in documentation that outlines that test setup runs per each test method? I tested in a dev edition and I'm only seeing the DML's run once per the entire test run which includes multiple test methods.
TestSetup only runs once.
Then the test methods run.
First I’m first