You need to sign in to do that
Don't have an account?

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:
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!
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); } }
Please try the below test class:
Thanks,
Maharajan.C
All Answers
Can you please share error details here. So that we can help you easily.
Thanks,
Maharajan.C
Please try the below test class:
Thanks,
Maharajan.C