+ Start a Discussion
degmodegmo 

Test data setup

Hi,
I am trying to create a test for a class that has if/else logic based on different lookup values for a field.  Below is a snippet of the code that I need to test.  I typcially would like to use Test Data Factory and testSetup when writing my unit tests.  I am trying to figure out the most efficient way to accomplish this.  

Should I create different student records with the different affliate lookup values in the Test Data Factory or is it more efficient to create one student record do a fetch a update with the different values in each test method?
 
if(student__c.affiliate__c == 'freshman') {
   // do stuff
}
else if(student__c.affiliate__c == 'sophomore') {
  // do stuff
}
else if (student__c.affiliate__c == 'junior') {
  // do stuff
}

Both student__c and affiliate__c are custom object.  There is a lookup r/ship b/n them.

 
Best Answer chosen by degmo
Andrew GAndrew G
In a way, it depends on what the code is doing.  

If part of the code behaviour to be tested is the transition from Freshman to Sophomore for the student record, then one Student record would be enough. And if you are only testing the assignment once, i would create the Affiliate record in each method.  But if there are multiple uses for each Affiliate record, then create them in the testSetup.  

If the code to be run is simply a doing something for each type of student, then a data creation of 3 x Student records each with a different associated Affiliate would do. And i would do that in the Testsetup.

If the code is to check some sort of insert event, then the data may need to be created an inserted in each test method.

Do a quick think or scribble on a pad, work out what are your test scenarios.  i.e. You want to test 6 things.  A positive and a negative for each affiliate relationship, then easy decision, all in Test setup.

Remember the main key for using @testSetup is the re-use of the test data.  It is quicker & more efficient for your test code to insert a record once and reset it in 2 test methods than it is to create that test record in 2 separate test methods.
If you find yourself building (or needing) the same data in each test method, then testSetup is where you want to be. 

HTH
Andrew
 

All Answers

RituSharmaRituSharma
Either way it's fine since test data is not stored in the database. You may go with the option of creating just one record in the test setup method. Then create separate method to test each scenario and update the record as per need.
Andrew GAndrew G
In a way, it depends on what the code is doing.  

If part of the code behaviour to be tested is the transition from Freshman to Sophomore for the student record, then one Student record would be enough. And if you are only testing the assignment once, i would create the Affiliate record in each method.  But if there are multiple uses for each Affiliate record, then create them in the testSetup.  

If the code to be run is simply a doing something for each type of student, then a data creation of 3 x Student records each with a different associated Affiliate would do. And i would do that in the Testsetup.

If the code is to check some sort of insert event, then the data may need to be created an inserted in each test method.

Do a quick think or scribble on a pad, work out what are your test scenarios.  i.e. You want to test 6 things.  A positive and a negative for each affiliate relationship, then easy decision, all in Test setup.

Remember the main key for using @testSetup is the re-use of the test data.  It is quicker & more efficient for your test code to insert a record once and reset it in 2 test methods than it is to create that test record in 2 separate test methods.
If you find yourself building (or needing) the same data in each test method, then testSetup is where you want to be. 

HTH
Andrew
 
This was selected as the best answer
Peachey MyersPeachey Myers
Thanks for the solution nox (https://www.nox.plus/)