+ Start a Discussion
Chinmay AbhangraoChinmay Abhangrao 

Why my test class fail when i user System.AssertEquals() method in it?

I have written this test class but it fails all the time and when I commit the system.assert();statement then it works fine.Anybody can explain where I am lagging? 

@istest public class TestingExam {
    @istest public static void Testing(){
        
        Account ac=new account();
        ac.Name='chinamyab';
        insert ac;
        
        case c=new case();
        c.AccountId=ac.Id;
        c.Status='working';
        c.Origin='phone';
        c.Product__c='GC1040';
        insert c;
        System.assertEquals(1,ac.Total_Issue_Count__c);
        System.assertEquals('GC1040',ac.Current_Issue__c);

    }

}
Niraj Kr SinghNiraj Kr Singh
Hi Chinmay,

According to your code here, your are refering Two fields of Account (1. Total_Issue_Count__c, 2. Current_Issue__c) in Assert method which might be getting updated by your Workflow/trigger while inserting Case record.

And that functionality is not working properly, check with that. Or provide your Workflow/trigger details here.

Let me know if not found solution.

Thanks
Niraj
Chinmay AbhangraoChinmay Abhangrao
This is the trigger i m firing now the scenario is when we insert the case then it the count will display on the respected account in the field( Total_Issue_Count__c)and in another field (Current_Issue__c)from an account it shows the name of the product for which issue/case get inserted.
 
trigger ProdTrackOnAcc on Case (after insert,after update,after delete) 
{  
    //TriggerOnCase__c newObj= TriggerOnCase__c.getInstance();
    //if( newObj.IsActive__c==true)
//{
       
             If(Trigger.isinsert && Trigger.isafter)
                    {
                       set<id> setid=new set<id>();
                        for(Case c:trigger.new )
                       {
                       setid.add(c.AccountId);    
                    } 
            list<account> acctoup=new list<account>();
            map<id,Account> acclist=new map<id,Account>([select id,Current_Issue__c,Past_Issue__c,Total_Issue_Count__c from account where id=:setid]);

    
    for(Case c:trigger.new)
        {    
             account a=acclist.get(c.AccountId);

            If(a.Total_Issue_Count__c==NULL)
                {
                   a.Total_Issue_Count__c=0;
                }
            a.Total_Issue_Count__c= a.Total_Issue_Count__c+1;
            
          
                 if(a.Current_Issue__c==null)
                {
                     a.Current_Issue__c= c.Product__c;
                }else if(a.Current_Issue__c!=null)
                    {
                          String toSet;           
                            toset= ','+c.Product__c;
                         String acur= a.Current_Issue__c;
                         a.Current_Issue__c=  acur+(toset);
                      System.debug('current issue is'+ a.Current_Issue__c);
                    }
              
                 acctoup.add(a);   
   //         EmailSendToOwner obj=new EmailSendToOwner();  
     //       String ownermailid=a.owner.email;
       //       String EmailIdOfOwner =acclist.get(c.AccountId).Owner.Email;
         //    obj.sendMail(EmailIdOfOwner, 'subject', 'body');
            //System.debug(EmailIdOfOwner); 
        }
            
            update acctoup;
    }
    
Steven NsubugaSteven Nsubuga
Update your test class to this
@istest public class TestingExam {
    @istest public static void Testing(){
        
        Account ac=new account();
        ac.Name='chinamyab';
        insert ac;
        
        case c=new case();
        c.AccountId=ac.Id;
        c.Status='working';
        c.Origin='phone';
        c.Product__c='GC1040';
        insert c;

		ac = [select id,Current_Issue__c,Total_Issue_Count__c from account limit 1];
        System.assertEquals(1,ac.Total_Issue_Count__c);
        System.assertEquals('GC1040',ac.Current_Issue__c);
    }

}

 
Chinmay AbhangraoChinmay Abhangrao
Not working  |||| this is the error :-
System.AssertException: Assertion Failed: Expected: 1, Actual: null​
 
Akshay_DhimanAkshay_Dhiman
Hi Chinmay,

Try the below code:
 
@isTest
 private class TestingExam {
    @isTest
 private static void Testing(){
        
        Account ac=new account();
        ac.Name='chinamyab';
  ac.Total_Issue_Count__c=1;    // fill this custom field
  ac.Current_Issue__c='GC1040';  //fill this custom  field
        insert ac;
        
        case c=new case();
        c.AccountId=ac.Id;
        c.Status='working';
        c.Origin='phone';
        c.Product__c='GC1040';
        insert c;
        System.assertEquals(1,ac.Total_Issue_Count__c);  //then check
  System.assertEquals('GC1040',ac.Current_Issue__c); //then check
    }
}

User-added image

IF you found this answer helpful then please mark it as best answer so it can help others.   

Thanks 
Akshay
Chinmay AbhangraoChinmay Abhangrao
@Akshay 
I have written trigger for that when a case raises then automatically the total issue count increases by 1.
Steven NsubugaSteven Nsubuga
Using this trigger
trigger ProdTrackOnAcc on Case (after insert,after update,after delete) 
{  
    //TriggerOnCase__c newObj= TriggerOnCase__c.getInstance();
    //if( newObj.IsActive__c==true)
    //{
    
    If(Trigger.isinsert && Trigger.isafter)
    {
        set<id> setid=new set<id>();
        for(Case c:trigger.new )
        {
            setid.add(c.AccountId);    
        } 
        list<account> acctoup=new list<account>();
        map<id,Account> acclist=new map<id,Account>([select id,Current_Issue__c,Past_Issue__c,Total_Issue_Count__c from account where id=:setid]);
        
        
        for(Case c:trigger.new)
        {    
            account a=acclist.get(c.AccountId);
            
            If(a.Total_Issue_Count__c==NULL)
            {
                a.Total_Issue_Count__c=0;
            }
            a.Total_Issue_Count__c= a.Total_Issue_Count__c+1;
            
            
            if(a.Current_Issue__c==null)
            {
                a.Current_Issue__c= c.Product__c;
            }else if(a.Current_Issue__c!=null)
            {
                String toSet;           
                toset= ','+c.Product__c;
                String acur= a.Current_Issue__c;
                a.Current_Issue__c=  acur+(toset);
                System.debug('current issue is'+ a.Current_Issue__c);
            }
            
            acctoup.add(a);   
            //         EmailSendToOwner obj=new EmailSendToOwner();  
            //       String ownermailid=a.owner.email;
            //       String EmailIdOfOwner =acclist.get(c.AccountId).Owner.Email;
            //    obj.sendMail(EmailIdOfOwner, 'subject', 'body');
            //System.debug(EmailIdOfOwner); 
        }
        
        update acctoup;
    }
}
and this class, everything works well.
@istest public class TestingExam {
    @istest public static void Testing(){
        
        Account ac=new account();
        ac.Name='chinamyab';
        insert ac;
        
        case c=new case();
        c.AccountId=ac.Id;
        c.Status='working';
        c.Origin='phone';
        c.Product__c='GC1040';
        insert c;
        
        ac = [select id,Current_Issue__c,Total_Issue_Count__c from account limit 1];
        System.assertEquals(1,ac.Total_Issue_Count__c);
        System.assertEquals('GC1040',ac.Current_Issue__c);
    }
    
}

 
Niraj Kr SinghNiraj Kr Singh
Hi Chinmay,

Now you need to update your test class and use this:
@istest public class TestingExam {
    @istest public static void Testing(){
        
        Account ac=new account();
        ac.Name='chinamyab';
        insert ac;
        
        case c=new case();
        c.AccountId=ac.Id;
        c.Status='working';
        c.Origin='phone';
        c.Product__c='GC1040';
        insert c;
		
		//Getting updated account by trigger after insertion of case.
		Account acRecord = [SELECT Id, Name, Total_Issue_Count__c, Current_Issue__c FROM Account WHERE Id =: ac.Id];
        
        System.assertEquals(1,acRecord.Total_Issue_Count__c);
        System.assertEquals(c.Product__c,acRecord.Current_Issue__c);
    }
}