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
Desiree DixonDesiree Dixon 

Confused Why Test Methods Failed

Hello! I'm just now digging into Apex Triggers, and I'm having a little trouble with creating tests. Here's my Apex class:
trigger CanceledProgram on Program__c (before update) {
	Program__c pNew = Trigger.new[0];
    Program__c pOld = Trigger.oldMap.get(pNew.id);
    
    if(pNew.Canceled__c != pOld.Canceled__c && pNew.Canceled__c == true){
        System.debug('Updated to True');
        System.debug('New value = ' + pNew.Canceled__c);
        System.debug('Old value = ' + pOld.Canceled__c);
        
        if(pOld.Name.length() <= 70){
            pNew.Name = 'CANCELED: ' + pNew.Name;
            System.debug('New name length: '+pNew.Name.length());
            System.debug('Old name length: '+pOld.Name.length());
            System.debug(pNew.Name);
        } else if (pOld.Name.length() > 70){
            String pNameShort = pNew.Name.substring(0, 65) + '...';
            pNew.Name = 'CANCELED: ' + pNameShort;
            System.debug(pNew.Name);
            System.debug(pNew.Name.length());
        }
        
        
    } else if (pNew.Canceled__c != pOld.Canceled__c && pNew.Canceled__c == false) {
        System.debug('Updated to False');
        System.debug('New value = ' + pNew.Canceled__c);
        System.debug('Old value = ' + pOld.Canceled__c);
        if(pNew.Name.startsWith('CANCELED: ')){
            pNew.Name = pNew.Name.substring(10);
        }
        
        
    } else {
        System.debug('The Canceled Field was not changed');
    }
    
    if (pNew.Name != pOld.Name && pOld.Name.length() >70){
        
        System.debug(pOld.Name.length());
        System.debug(pNew.Name.length());
    }
        
}

And see below for the test class. It says I have 100% coverage, but that the 3/4 tests failed. I assume they need to succeed in order to push this class to production. I don't understand why they didn't pass though? In the first test method, I did a little testing, trial, and error with using System.debug to double check the values, and the test should be passing. Can anyone tell me what I'm doing wrong? Thanks for any help you can provide!
 
@isTest
private class CanceledProgramTest {     

    @isTest static void TestCanceledProgramWithShortName() {
        Program__c p1 = new Program__c(Name='Will not happen');
        insert p1;
        
        p1 = [SELECT Id, Name, Canceled__c FROM Program__c WHERE Id = :p1.Id];
        System.debug(p1);
        Test.startTest();
        p1.Canceled__c = true;
        update p1;
        System.assertEquals('CANCELED: Will not happen', p1.Name);
        Test.stopTest();
    }
    
    @isTest static void TestCanceledProgramWithLongName() {
        Program__c p = new Program__c(Name='This program will not happen. I am sorry, but this program just will not happen.');
        insert p;
        p.Canceled__c = true;
        update p;
        System.assertEquals('CANCELED: This program will not happen. I am sorry, but this program just w...', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramChangeName() {
        Program__c p = new Program__c(Name='CANCELED: We will have it after all. Name changes.', Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        System.assertEquals('We will have it after all. Name changes.', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramSameName() {
        Program__c p = new Program__c(Name='We will have it after all. Name is same.' , Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        System.assertEquals('We will have it after all. Name is same.', p.Name);
    }
}

 
Best Answer chosen by Desiree Dixon
Maharajan CMaharajan C
Hi Dixon,

Please try the below test class:
 
@isTest
private class CanceledProgramTest {     

    @isTest static void TestCanceledProgramWithShortName() {
        Project__c p1 = new Project__c(Name='Will not happen');
        insert p1;
        
        System.debug(p1);
        Test.startTest();
        p1.Canceled__c = true;
        update p1;
        
        p1 = [SELECT Id, Name, Canceled__c FROM Project__c WHERE Id = :p1.Id];
        System.assertEquals('CANCELED: Will not happen', p1.Name);
        Test.stopTest();
    }
    
    @isTest static void TestCanceledProgramWithLongName() {
        Project__c p = new Project__c(Name='This program will not happen. I am sorry, but this program just will not happen.');
        insert p;
        p.Canceled__c = true;
        update p;
        p = [SELECT Id, Name, Canceled__c FROM Project__c WHERE Id = :p.Id];
        System.assertEquals('CANCELED: This program will not happen. I am sorry, but this program just w...', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramChangeName() {
        Project__c p = new Project__c(Name='CANCELED: We will have it after all. Name changes.', Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        p = [SELECT Id, Name, Canceled__c FROM Project__c WHERE Id = :p.Id];
        System.assertEquals('We will have it after all. Name changes.', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramSameName() {
        Project__c p = new Project__c(Name='We will have it after all. Name is same.' , Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        System.assertEquals('We will have it after all. Name is same.', p.Name);
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Dixon,

Can you please  share error details here. So that we can help you easily.

Thanks,
Maharajan.C
Maharajan CMaharajan C
Hi Dixon,

Please try the below test class:
 
@isTest
private class CanceledProgramTest {     

    @isTest static void TestCanceledProgramWithShortName() {
        Project__c p1 = new Project__c(Name='Will not happen');
        insert p1;
        
        System.debug(p1);
        Test.startTest();
        p1.Canceled__c = true;
        update p1;
        
        p1 = [SELECT Id, Name, Canceled__c FROM Project__c WHERE Id = :p1.Id];
        System.assertEquals('CANCELED: Will not happen', p1.Name);
        Test.stopTest();
    }
    
    @isTest static void TestCanceledProgramWithLongName() {
        Project__c p = new Project__c(Name='This program will not happen. I am sorry, but this program just will not happen.');
        insert p;
        p.Canceled__c = true;
        update p;
        p = [SELECT Id, Name, Canceled__c FROM Project__c WHERE Id = :p.Id];
        System.assertEquals('CANCELED: This program will not happen. I am sorry, but this program just w...', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramChangeName() {
        Project__c p = new Project__c(Name='CANCELED: We will have it after all. Name changes.', Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        p = [SELECT Id, Name, Canceled__c FROM Project__c WHERE Id = :p.Id];
        System.assertEquals('We will have it after all. Name changes.', p.Name);
    }
    
    @isTest static void TestUnCanceledProgramSameName() {
        Project__c p = new Project__c(Name='We will have it after all. Name is same.' , Canceled__c = true);
        insert p;
        p.Canceled__c = false;
        update p;
        System.assertEquals('We will have it after all. Name is same.', p.Name);
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
Desiree DixonDesiree Dixon
That worked! Thanks so much Maharajan, so it looks like I needed to reassign p after updating the record, instead of before. 
Maharajan CMaharajan C
Yes you are correct...
Waleed Khan 8Waleed Khan 8
Yes this Worked. Also got help from simulation (https://carparkingmultiplayerapk.com/car-parking-multiplayer-mod-apk/) based view.
Hjams HaksHjams Haks

Hello Desiree,
Maharajan rightly pointed out the primary issue you were facing in your test methods. When dealing with Salesforce triggers and subsequent DML operations in a test context, it's always vital to re-query the record after performing a DML operation (like update) to get the most recent state of the record. This ensures that you're validating against the latest data that has been acted upon by triggers, workflows, process builders, etc.

 

By the way if you want to know more about IOS Mod Apk You can check it here  (https://carparkapk.com/download-car-parking-multiplayer-mod-apk-for-ios/)