function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Doria Hamelryk VetranoDoria Hamelryk Vetrano 

your advice concerning test class on triggers

Hi there,
I just began to create test classes and I had a question about the good practices.

Assume we have a trigger that updates a "LastOrderDate__c" on Account, based on the field EffectiveDate in Order object.
We can have different cases :
  • We delete an order - Account had only this order => LastOrderDate__c becomes null
  • We delete an order - Account had several orders => LastOrderDate__c becomes the max(EffectiveDate) of his other orders
  • We create/update an order => LastOrderDate__c becomes the max(EffectiveDate) of all his orders
My question is :
Should I make only one test class including all the steps to cover all the cases ?
  • test class
    • create order 1
    • create order 2
    • delete order 1
    • delete order 2
Or should I make one test class for each case :
  • test class1
    • create order
  • test class2
    • create order
    • delete order
  • test class3
    • create order1
    • create order2
    • delete order1
    • delete order2
I ask this question because the real purpose of this class test is to check if the value of LastOrderDate is correct.
Should I then test with System.assertEquals the value of LastOrderDate after each action (create/delete) ?

Thanks for your help
Best Answer chosen by Doria Hamelryk Vetrano
Manj_SFDCManj_SFDC
Hey Doria, 

for this scenario you can create a single test class and yes you can use System.assertEquals ( you may decide to use this based on the controller/class logic).
Also if you have multiple classes which may need similar testing , you can create a utility class wherein you can create the common test data, may creation of the order.

Good luck !

All Answers

Manj_SFDCManj_SFDC
Hey Doria, 

for this scenario you can create a single test class and yes you can use System.assertEquals ( you may decide to use this based on the controller/class logic).
Also if you have multiple classes which may need similar testing , you can create a utility class wherein you can create the common test data, may creation of the order.

Good luck !
This was selected as the best answer
Doria Hamelryk VetranoDoria Hamelryk Vetrano
Thanks! That's what I will do :)