0

I am somewhat of an Apex newbie and I am running into issues with the following steps. My test class was passing correctly but after some feedback I was instructed to place all my assertions after the Test.stopTest();

The issue is that I am testing that values change on multiple sObject records as the process goes through. I tried storing these as string/datetime values as I went along but when I run the assertions at the end of the process these are null. Example below from the bottom of the method failing:

        insert processStarter;

        //This will have modified the record below so need to compare it
        ParentRecord = [SELECT Field_A__c, Field_B__c FROM Parent_Object__c WHERE Id =: ParentRecord.Id];

        //Grab the values for an assertion later
        final DateTime FieldA1 = ParentRecord.Field_A__c;
        final Date FieldB2 = ParentRecord.Field_B__c;

//Create new record which will start the process again
new Random_Object__c ProcessStarter2 = new Random_Object__c( Parent__c = ParentRecord.Id, Other_Field__c = 'This will kick off a different process');
insert ProcessStarter2;

//Now we check again for the changed records
ParentRecord = [SELECT Field_A__c, Field_B__c, Field_C__c, Field_D__c FROM Parent_Object__c WHERE Id =: ParentRecord.Id];

        //Test stops here
        Test.stopTest();

        //THIS IS WHERE THE ASSERTION FAILS SINCE IT THINKS THE VARIABLE FIELDA1 IS NULL
        System.assertEquals(processStarter.Field_A__c, FieldA1);
        System.assertEquals(processStarter.Field_B__c, FieldB2);

        //ASSERT THE FIELDS ARE CORRECT THE SECOND TIME
        System.assertEquals(processStarter2.Field_C__c, ParentRecord.Field_A__c);
        System.assertEquals(processStarter2.Field_D__c, ParentRecord.Field_B__c);

I am used to checking the process results as I go so I've never run into this issue before. Can someone please provide some clarity?

Thank you!

1 Answer 1

1

You need to break up your test into multiple methods when doing assertions like this: (Code is not complete as you only had partial code posted, but is meant for illustration only)

For example:

@isTest
private class myTests{

  private static test method void myFirstTest(){
    test.startTest();
       insert processStarter;
    test.stopTest();

        //This will have modified the record below so need to compare it
        ParentRecord = [SELECT Field_A__c, Field_B__c FROM Parent_Object__c WHERE Id =: ParentRecord.Id];

        //Grab the values for an assertion later
        final DateTime FieldA1 = ParentRecord.Field_A__c;
        final Date FieldB2 = ParentRecord.Field_B__c;

        System.assertEquals(processStarter.Field_A__c, FieldA1);
        System.assertEquals(processStarter.Field_B__c, FieldB2);

  }


  private static testMethod void mySecondTest(){

     test.startTest();
        //Create new record which will start the process again
        new Random_Object__c ProcessStarter2 = new Random_Object__c( Parent__c =        
          ParentRecord.Id, Other_Field__c = 'This will kick off a different  
          process');
        insert ProcessStarter2;
     test.stopTest();

      //Now we check again for the changed records
ParentRecord = [SELECT Field_A__c, Field_B__c, Field_C__c, Field_D__c FROM Parent_Object__c WHERE Id =: ParentRecord.Id];


      //ASSERT THE FIELDS ARE CORRECT THE SECOND TIME
        System.assertEquals(processStarter2.Field_C__c, ParentRecord.Field_A__c);
        System.assertEquals(processStarter2.Field_D__c, ParentRecord.Field_B__c);


  }

}

It is always best practice to break your test up into functional unit test where you test a business process, function, etc within one method. Also adding negative test cases as well covering all possibilities and outcomes.

The main reason (other than good form) you want to do assertions AFTER test.stopTest() is so that any async processes are completed before you attempt to do assertions.

2
  • There are no ways to store the values to do it as one test method? I understand the logic of doing it in distinct methods but that also adds to the running time of the test class and I know there must be a way to store these for future comparison. This particular trigger test class already has 20+ test methods so i don't want to have to to start breaking portions into further portions since there is data setup involved in each case. Thank you for taking the time to answer!
    – cloudcap
    Commented Oct 12, 2015 at 14:28
  • @domo13 - If you need to do it that way then assert your value like your were before you moved it outside of the stop test. Seems like the process is updating the same parent record so you have to assert before every run or the process.
    – Eric
    Commented Oct 12, 2015 at 15:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .