You need to sign in to do that
Don't have an account?
Mark_Kaprielian
How to update a single field as part of an Apex Trigger test
Hello All
I'm new to Apex. I have successfully created a Case Trigger and a Class which I call from it. I have 100% coverage of my Class methods. I now need to write test coverage for my Case Trigger code. Classes and Methods seem straight forward to me as it's pure calls. For the Case Trigger code, I need to do things to the system to have it invoke things. This I do not know how to do despite trying to find examples.
I believe a simple example would help me understand the technique and in fact would be the solution I need for this my first attempt at Trigger testing.
My Case trigger is to designed to take action when the Case Status field changes.
My trigger test needs to change the status to traverse the different actions that are taken based on the new status.
Mark
I'm new to Apex. I have successfully created a Case Trigger and a Class which I call from it. I have 100% coverage of my Class methods. I now need to write test coverage for my Case Trigger code. Classes and Methods seem straight forward to me as it's pure calls. For the Case Trigger code, I need to do things to the system to have it invoke things. This I do not know how to do despite trying to find examples.
I believe a simple example would help me understand the technique and in fact would be the solution I need for this my first attempt at Trigger testing.
My Case trigger is to designed to take action when the Case Status field changes.
My trigger test needs to change the status to traverse the different actions that are taken based on the new status.
- Do I need to create a new case record for each test then change the status before I loose the scope of the test?
- Note there are several required fields I would need to satisfy to create a new case record
- If yes to the above how can I push the creation of the record into a sub-class and then reference that case record while still in the scope of the test classes?
Mark
Depending on the structure of your trigger, it should be very simple to achieve code coverage. Everyone is slightly different in how they code, but I choose to keep my triggers as simple as possible. Example: As seen, I have minimal code in my trigger. Similarly, my Handler method do the minimal required and call other methods to keep the reading of the Handler as simple as possible.
So, now the question on your testing.
If you have common Test data that is relevant to the entire testing code, then use of @testSetup is recommended. When data is setup this way, regardless what you do during each test method, the data will "reset" for the next test method. So, if the test Case record is created with a Status of "new" and the first test method changes that to "old", when the test data is queried in the next test method, it will be back to "new".
Example of using @TestSetup.
Note that I am using Test Helper class to create my test data, but the test data can also be explicitly created in the @testSetup section.
Now, if we look at the testing.
I am running 2 test methods. One for the Insert , One for the Update. Depending on the complexity of the trigger, I generally run one test method for each method and for each branch of the method. So if there is an IF .... ELSE .... branch, i would have a test for each branch
so: The test methods would look like
for completeness, here is the testclass helper
Note that I return lists of records from my test class helper, that way they are generic and I can tweak them if need for each actual test before the insert. Example:
Hope the above provides some help and clarity
Regards
Andrew
All Answers
Depending on the structure of your trigger, it should be very simple to achieve code coverage. Everyone is slightly different in how they code, but I choose to keep my triggers as simple as possible. Example: As seen, I have minimal code in my trigger. Similarly, my Handler method do the minimal required and call other methods to keep the reading of the Handler as simple as possible.
So, now the question on your testing.
If you have common Test data that is relevant to the entire testing code, then use of @testSetup is recommended. When data is setup this way, regardless what you do during each test method, the data will "reset" for the next test method. So, if the test Case record is created with a Status of "new" and the first test method changes that to "old", when the test data is queried in the next test method, it will be back to "new".
Example of using @TestSetup.
Note that I am using Test Helper class to create my test data, but the test data can also be explicitly created in the @testSetup section.
Now, if we look at the testing.
I am running 2 test methods. One for the Insert , One for the Update. Depending on the complexity of the trigger, I generally run one test method for each method and for each branch of the method. So if there is an IF .... ELSE .... branch, i would have a test for each branch
so: The test methods would look like
for completeness, here is the testclass helper
Note that I return lists of records from my test class helper, that way they are generic and I can tweak them if need for each actual test before the insert. Example:
Hope the above provides some help and clarity
Regards
Andrew
It will take me some time to work through your solution but it looks like I should be able to both learn and execute from the work you did to answer my questions. Thank you for putting it all in perspective.
Great work, I really appreciate you taking the time to provide this material.
Mark