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
SeksakiSeksaki 

write test class for trigger

Hello,
can you help me write a test for this trigger plz
trigger ContractTrigger on Contract (after update) {
    Public String[] statut_activated   {set;get;}
    Public String[] statut_waiting   {set;get;}
    Public String[] statut_terminated   {set;get;}
    
    statut_activated = new String[] {'Accepted','Accepted2'};
    statut_waiting = new String[] {'waiting','waiting2'}; 
    statut_terminated = new String[] {'terminated','terminated2'}; 

    
    List<Decimal> listAmount= new List<Decimal>();
	set<Id> AccountIds = new set<Id>();
    for(Contract c : Trigger.new){
    if(c.AccountId != null)
    	AccountIds.add(c.AccountId);
    }

  

    Map<String, Contract> cntrMap  = new Map<String, Contract>();
    
    for(Contract contr : [Select Id,contract_stat__c,amount__c From Contract Where AccountId IN: AccountIds]){
        cntrMap.put(contr.Id , contr);
         
         Account acc = [SELECT Id,st__c FROM Account WHERE Id IN: AccountIds];	
        if(contr.amount__c > 0){
            listAmount.add(contr.amount__c);
        }else{
          acc.checked__c=FALSE;
          update acc;
        }
        
       
        if(statut_activated.contains(contr.contract_stat__c)){
             cntrMap.put(contr.contract_stat__c , contr);     
             acc.st__c='customer';   
             update acc;
             break;
            
            /* Contrat En attente*/ 
        }else if(statut_waiting.contains(contr.contract_stat__c)){

             acc.st__c='customer_type2';
             update acc;
             break;
        /*  Contrat En Terminé  */ 
        }else if(statut_terminated.contains(contr.contract_stat__c)){
		
             acc.st__c='customer_type3';
             update acc;
             break;     
        }else{

             acc.st__c='customer_other';
             update acc;       
        }
    }

    for(Decimal l : listAmount){
    	if(l > 0){
          Account acc = [SELECT Id,st__c FROM Account WHERE Id IN: AccountIds];	
          acc.checked__c=TRUE;
          update acc;
         }
	}

}

Thanks for help 
Best Answer chosen by Seksaki
Daniel AhlDaniel Ahl

Hello there Seksaki!
Your trigger on Contract is on "after update" but in your testclass you are not triggering an update on the contract-record.

Either change the "after update" to "after insert" or make sure that you update the contract in your test-class so that the trigger is being triggered!

 

All Answers

SeksakiSeksaki
This is my test class, thant not coverage code:
@isTest
public class ContractTrigger {
	@isTest static void myUnitTest() {
        	String pid;
            Integer i=1;
           Test.startTest(); 
           Account acct = new Account(Name='Test Account',Ref_client__c='FFFF1');
       	   insert acct;
            
           List<Contract> cnt = new List<Contract>();
           // for(Integer i=0;i<3;i++) {
                Contract c = new Contract(Name=acct.Name + ' Contract' + i,
                                       StartDate=System.today().addMonths(1),
                                       Status='Draft',
                                       contract_stat__c='waiting',
                                       AccountId=acct.Id);
            	cnt.add(c);
           //}
           insert cnt;
        
       		List<Contract> contra = [Select Id,contract_stat__c,amount__c ,AccountId From Contract Where AccountId = : acct.id];
	        System.assertEquals(1, contra.size());
	     
        	System.assertEquals(acct.Id, contra[0].AccountId); 
        	
            acct.Impay__c=TRUE;
            acct.Statut_client__c='lead';
            update acct;
  			
            System.assertEquals('lead', acct.Statut_client__c);
        	Test.stopTest();
    }
}

 
Daniel AhlDaniel Ahl

Hello there Seksaki!
Your trigger on Contract is on "after update" but in your testclass you are not triggering an update on the contract-record.

Either change the "after update" to "after insert" or make sure that you update the contract in your test-class so that the trigger is being triggered!

 

This was selected as the best answer
Daniel AhlDaniel Ahl
Hello again, 
I've added to your test-class so that it covers 100% of your trigger, if you add (after update, after insert) to the trigger.
trigger ContractTrigger on Contract (after update, after insert) {
    Public String[] statut_activated   {set;get;}
    Public String[] statut_waiting   {set;get;}
    Public String[] statut_terminated   {set;get;}
    
    statut_activated = new String[] {'Accepted','Accepted2'};
    statut_waiting = new String[] {'waiting','waiting2'}; 
    statut_terminated = new String[] {'terminated','terminated2'}; 

    
    List<Decimal> listAmount= new List<Decimal>();
    set<Id> AccountIds = new set<Id>();
    for(Contract c : Trigger.new){
    if(c.AccountId != null)
        AccountIds.add(c.AccountId);
    }

  

    Map<String, Contract> cntrMap  = new Map<String, Contract>();
    
    for(Contract contr : [Select Id,contract_stat__c,amount__c From Contract Where AccountId IN: AccountIds]){
        cntrMap.put(contr.Id , contr);
         
         Account acc = [SELECT Id,st__c FROM Account WHERE Id IN: AccountIds];    
        if(contr.amount__c > 0){
            listAmount.add(contr.amount__c);
        }else{
          acc.checked__c=FALSE;
          update acc;
        }
        
       
        if(statut_activated.contains(contr.contract_stat__c)){
             cntrMap.put(contr.contract_stat__c , contr);     
             acc.st__c='customer';   
             update acc;
             break;
            
            /* Contrat En attente*/ 
        }else if(statut_waiting.contains(contr.contract_stat__c)){

             acc.st__c='customer_type2';
             update acc;
             break;
        /*  Contrat En Terminé  */ 
        }else if(statut_terminated.contains(contr.contract_stat__c)){
        
             acc.st__c='customer_type3';
             update acc;
             break;     
        }else{

             acc.st__c='customer_other';
             update acc;       
        }
    }

    for(Decimal l : listAmount){
        if(l > 0){
          Account acc = [SELECT Id,st__c FROM Account WHERE Id IN: AccountIds];    
          acc.checked__c=TRUE;
          update acc;
         }
    }

}

@isTest
public class ContractTrigger {
	@isTest static void myUnitTest() {
        	String pid;
            Integer i=1;
           Test.startTest(); 
           Account acct = new Account(Name='Test Account',Ref_client__c='FFFF1');
       	   insert acct;
            
           List<Contract> cnt = new List<Contract>();
           // for(Integer i=0;i<3;i++) {
                Contract c = new Contract(Name=acct.Name + ' Contract' + i,
                                       StartDate=System.today().addMonths(1),
                                       Status='Draft',
                                       contract_stat__c='waiting',
                                       AccountId=acct.Id);
            	cnt.add(c);
           //}
           insert cnt;
        
       		List<Contract> contra = [Select Id,contract_stat__c,amount__c ,AccountId From Contract Where AccountId = : acct.id];
	        System.assertEquals(1, contra.size());
	     
        	System.assertEquals(acct.Id, contra[0].AccountId); 
        	
            acct.Impay__c=TRUE;
            acct.Statut_client__c='lead';
            update acct;
        
  			contra[0].amount__c = 1;
        	contra[0].contract_stat__c = 'Accepted';
        	update contra;
        
        	contra[0].contract_stat__c = 'terminated';
        	update contra;
        
        	contra[0].contract_stat__c = 'else';
        	update contra;
            System.assertEquals('lead', acct.Statut_client__c);
        	Test.stopTest();
    }
}


 
SeksakiSeksaki
Hello Daniel,

indeed, when you said after insert it worked, but do you have to add after insert even if I don't use it?

thanks a lot for your help Daniel.
Daniel AhlDaniel Ahl
Hello again Seksaki!
No, you do not need to have the "after insert" if the trigger is only supposed to be used on before the records are inserted to the database.

You are welcome! :)