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
Tasia Demuth 4Tasia Demuth 4 

Assertion failed error in test class

Hi all, 

I am getting an assertion failed in the logs when I run the below test. I receive 100% coverage on this test. Any help you can provide to point me in the right direction, would be much appreciated. 

Thanks!
Tasia

@isTest
public class UpdateCaseWhenLeadDeletedTest {

    private static testMethod void LeadDeletedTest(){
        //create a Lead
        Lead lead=new Lead(LastName='Test',FirstName='Tester',Status='Known', Company = 'CD Test');
        insert lead;
        // create a Case
        Case cscase=new Case(Status = 'New', Priority = 'Medium', Origin = 'Email');
        cscase.Lead__c = lead.Id;
        insert cscase;
       
        //delete Lead
        delete lead;
        
        List<Case> lstCase = [SELECT id ,Status,Type FROM Case WHERE Lead__c =:lead.id ];
        System.assert(lstCase.size() > 0 );
        System.assertEquals('Denied', lstCase[0].Status);
        System.assertEquals('Web Account Denied', lstCase[0].Type);
    }
}
Best Answer chosen by Tasia Demuth 4
Alain CabonAlain Cabon
Hi Tasia,

Did you want to test a trigger (ondelete for lead) ?

Otherwise the deletion of the lead will "nullify" the lead__c in Case by default.

       delete Lead;
           
        List<Case> lstCase = [SELECT id ,Status,Type FROM Case WHERE Lead__c = null ];
   
        System.assert(lstCase.size() > 0 );
        System.assertEquals('New', lstCase[0].Status);            // or value changed by a trigger?
        System.assertEquals(null, lstCase[0].Type);                 // or value changed by a trigger?
 

All Answers

Alain CabonAlain Cabon
Hi Tasia,

Did you want to test a trigger (ondelete for lead) ?

Otherwise the deletion of the lead will "nullify" the lead__c in Case by default.

       delete Lead;
           
        List<Case> lstCase = [SELECT id ,Status,Type FROM Case WHERE Lead__c = null ];
   
        System.assert(lstCase.size() > 0 );
        System.assertEquals('New', lstCase[0].Status);            // or value changed by a trigger?
        System.assertEquals(null, lstCase[0].Type);                 // or value changed by a trigger?
 
This was selected as the best answer
Tasia Demuth 4Tasia Demuth 4
Yes, I was trying to test a trigger that updates the case when a lead is deleted. So the trigger updates Lead__c, Status, and Type on the case when a lead is deleted. 

I will try your above suggestion shortly! 

Thanks, 
Tasia