+ Start a Discussion
ashu 6112ashu 6112 

Test Class trigger

Hi All, I had a requirement in which there were two objects ABC and DEF with master detail relation, ABC as master. Both objects hav a checkbox field.Abc has Has_condition__c checkbox whereas Def has Active__c checkbox. NOw I had to write the trigger if anyone of the child record has checkbox=true, then master record must have checkbox true, otherwise false if there is no child record with checkbox=true. I wrote the trigger for this and that is working fine. NOw I have to wite test class for this using fatory data class. Please help me out in this..

trigger hasActiveCheckboxCondition on Def__c (After insert, After update)
{   
    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
    {
            hasActiveCheckboxDef_Abc t = new hasActiveCheckboxDef_Abc();
            t.insertCondition(trigger.new);
            
    }  
}        
********************************
Apex class:

public class hasActiveCheckboxDef_Abc
{
    public void insertdef(List<def__c> tab1)
    {
        Set<Id> stemp = new Set<Id >();
    for(def__c c : tab1)
    {
        stemp.add(c.abc__c);
    }
    
    //Map<id,List<def__c>> mpDb=new Map<id,List<def__c>>();
    List<abc__c> tabToBeUpdated = new List<abc__c>();
    Map<id, abc__c> mapId = new Map<id,abc__c>();
       
    for(abc__c tab : [select id,Has_Condition__c,(Select id,name,abc__c,Active__c from abcs__r) from abc__c where id IN :stemp])
    {
          
        Boolean flag = false;           
        for(def__c cond : tab.defs__r)
        {
            if(cond.Active__c) {
                flag = true;
                tab.Has_Condition__c = true;
                break;
             }
            
            
        }
        if(flag == false)
        {
          tab.Has_Condition__c = false;  
        }
        mapId.put(tab.id,tab);
           
    }
    if(mapId != null)
    update mapId.values();
    }
}
 
Best Answer chosen by ashu 6112
ShawnHepkerShawnHepker
public static testMethod void testMasterRecordUpdate(){
   ABC__c master = new ABC__c();
   DEF__c detail = new DEF__c();

   master.name = 'Master';
   master.Has_condition__c = false;
   insert master;

   detail.name = 'detail';
   detail.active__c = false;
   detail.relationshipfield__c = master.id;
   insert detail;

   master = [select id, Has_Condition__c from ABC__c LIMIT 1];
   system.assertEquals(false, master.Has_Condition__c);

   test.startTest();
      detail.active__c = true;
      update detail;
   test.stopTest();

   master = [select id, Has_Condition__c from ABC__c LIMIT 1];
   system.assertEquals(true, master.Has_Condition__c);

}

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I hope you are calling hasActiveCheckboxDef_Abc class from hasActiveCheckboxCondition  trigger ?

Sample Test Factory Class
public abstract class TestDataFactory 
{
    
    public static Abc__c createAbc( ) 
	{
        Abc__c = oAbc = new Abc__c();
		oAbc.name = 'test abc';
		// add data for all required fields
		insert oAbc;
		
		return oAbc;
    }
	
    public static Def__c createDef( Abc__c oAbc) 
	{
		Def__c oDe = new Def__c();
        oDe.Name = 'test account';
		oDe.number__c = 1234;
		oDe.Abc__c  = oAbc.Id;
        // add all required fields
        insert oDe ;
		return oDe;
	}
	
}
Pleass try below test class
@isTest
public class hasActiveCheckboxConditionTest 
{
    public static testMethod void testMyController() 
	{
		Abc__c oAbc = TestDataFactory.createAbc();
		Def__c oDe = TestDataFactory.createDef(oAbc);
        
        oDe.number__c = 3216 ;
		update oDe ;
		
		

    }
}


 
ashu 6112ashu 6112
Yes, I am calling this class from trigger, but here requirement is that  if anyone of the child record has checkbox, Has_condition__c =true, then master record must have checkbox, Active__c=true, otherwise false if there is no child record with checkbox, Has_condition__c =true. I have written the above trigger for that, now I have to write test class for teh above trigger.
ShawnHepkerShawnHepker
public static testMethod void testMasterRecordUpdate(){
   ABC__c master = new ABC__c();
   DEF__c detail = new DEF__c();

   master.name = 'Master';
   master.Has_condition__c = false;
   insert master;

   detail.name = 'detail';
   detail.active__c = false;
   detail.relationshipfield__c = master.id;
   insert detail;

   master = [select id, Has_Condition__c from ABC__c LIMIT 1];
   system.assertEquals(false, master.Has_Condition__c);

   test.startTest();
      detail.active__c = true;
      update detail;
   test.stopTest();

   master = [select id, Has_Condition__c from ABC__c LIMIT 1];
   system.assertEquals(true, master.Has_Condition__c);

}

 
This was selected as the best answer
ashu 6112ashu 6112
Hi Shawn,
Many Thnaks for the sol.
Could you please expalin the use of test.startTest() n test.stopTest() mehods.

Thank,
ashu 6112ashu 6112
Shwan, Also I need factory data class for this for the claification, I would be very thankful.
ShawnHepkerShawnHepker
When writing a method to create test data it really just depends on what fields you would like to populate. For example if you just want to pupulate name and has_condition__c field for the abc object then you would pass in two parameters to the method that is creating the abc object. However if you are trying to create more than one abc record and dont care about the name or has_condition__c field then you only need to pass in an integer for the number of records to create, then write a for loop to create the given number of records.

To make the utility class as reusable as possible (for other tests) you will have to determine what needs to be passed in when creating the records. Once you know that you can just reuse the majority of the code where I create the abc__c and def__c records (lines 5 - 12).