+ Start a Discussion
SK SGSSK SGS 

Creating Test Trigger

Hi All,
How to create the Test class for triggers?can you any one explain?and below is the sample trigger,then how to create the test class for this.
  trigger CheckForDefaultCustAddress on CustomerAddress__c (before insert,before update) 
{
    Set<String> shipAddress = new Set<String>();
    Set<Id> accountId = new Set<Id>();
    Map<String, CustomerAddress__c> customerAddressMap = new Map<String, CustomerAddress__c>();
    Map<String, CustomerAddress__c> accountCustomerAddress = new Map<String, CustomerAddress__c>();
    Map<String, CustomerAddress__c> accountDefaultCustomerAddress = new Map<String, CustomerAddress__c>();
    
    For(CustomerAddress__c p : Trigger.New)
    {
        
        If(p.BillingAddress__c != null)
        {
            shipAddress.add(p.BillingAddress__c);
        }
        If(p.CustomerName__c!= null)
        {
            accountId.add(p.CustomerName__c);
        }
    }
    //to check Same address is present in customer 
    For(CustomerAddress__c p :[SELECT Id,BillingAddress__c,CustomerName__c FROM CustomerAddress__c WHERE BillingAddress__c =:shipAddress AND CustomerName__c =: accountId])
    {
        customerAddressMap.put(p.BillingAddress__c+'-'+p.CustomerName__c, p);
    }
    
    // Check for Address is there for Account OR not
    For(CustomerAddress__c p :[SELECT Id,CustomerName__c FROM CustomerAddress__c WHERE CustomerName__c =: accountId])
    {
        accountCustomerAddress.put(p.Id+'-'+p.CustomerName__c, p);
    }
    
    For(CustomerAddress__c p :[SELECT Id,CustomerName__c FROM CustomerAddress__c WHERE CustomerName__c =: accountId AND PrimaryDefaultAddress__c = TRUE])
    {
        accountDefaultCustomerAddress.put(p.Id+'-'+p.CustomerName__c, p);
    }
    
    
    For(CustomerAddress__c p : Trigger.New)
    {
        String keyChek = p.BillingAddress__c+'-'+p.CustomerName__c;
        
        if(accountCustomerAddress.size()<1)
        {
            System.debug('no address first one so make it default');
            p.PrimaryDefaultAddress__c=true;
        }   
        else if(Trigger.isInsert && p.PrimaryDefaultAddress__c)
        {
            if(accountDefaultCustomerAddress.size()>=1)
            {
                System.debug('already Default Address is there');
                p.name.addError('One Account have only one Default Address');
            }    
            
        }        
        else if(Trigger.isInsert && customerAddressMap.containsKey(keyChek))
        {
                 System.debug('already same address is there');  
                 p.name.addError('already same address is there');
        }
        
       
    }     
}
thanks
Raj VakatiRaj Vakati
try this
 
@isTest
Private Class CheckForDefaultCustAddressTest
{

	static Testmethod void CheckForDefaultCustAddressEx()
	{
	
		
		Test.startTest();
		
CustomerAddress__c  co = new CustomerAddress__c () ;
co.Name ='Test' ;
co.BillingAddress__c ='USA';
co.CustomerName__c='Test';
co.PrimaryDefaultAddress__c =false ; 
insert co ; 

CustomerAddress__c  co1 = new CustomerAddress__c () ;
co1.Name ='Test' ;
co1.BillingAddress__c ='USA';
co1.CustomerName__c='Test';
col.PrimaryDefaultAddress__c = true ;
insert co1 ; 


CustomerAddress__c  co2 = new CustomerAddress__c () ;
co2.Name ='Test' ;
co2.PrimaryDefaultAddress__c =true ;
co2.BillingAddress__c ='USA';
co2.CustomerName__c='Test';
insert co2	 ; 



co2.PrimaryDefaultAddress__c =false ;
co2.BillingAddress__c ='UK';
co2.CustomerName__c='Testuser';
update co2	 ; 
		 
          Test.StopTest();
	}
}

 
Ajay K DubediAjay K Dubedi
Hi SK,
You can try the following code hope this will help you. Please add the required fields of CustomerAddress__c as I was unware of the required field.

@isTest
Private Class CheckForDefaultCustAddress_Test
{
    @isTest static void Testmethod1(){
        Test.startTest();
        CustomerAddress__c  c = new CustomerAddress__c();
        c.CustomerName__c = 'Test Name';
        c.BillingAddress__c = 'Test BillingAddress';
        c.CustomerAddress__c = 'Test CustomerAddress';
        c.PrimaryDefaultAddress__c = false;
        
        insert c;
        
        CustomerAddress__c  cObj = new CustomerAddress__c();
        cObj.CustomerName__c = 'Test Name';
        cObj.BillingAddress__c = 'Test BillingAddress';
        cObj.CustomerAddress__c = 'Test CustomerAddress';
        cObj.PrimaryDefaultAddress__c = true;
        
        insert cObj;
                                   
        Test.StopTest();
    }    
}

Thanks,
Ajay Dubedi