+ Start a Discussion
john2john2 

Need Help with test class

I am writing a test class for an Update trigger.  I can save with no error messages, but when I run the test I get an error message and its covering 60% only.

 

here is the error: Class.TestupdatePrimaryAddress.testClass: line 11, column 10 External entry point 


Here is the test class.

 

 

@isTest
private class TestupdatePrimaryAddress
{
    static testMethod void testClass()
    {   
        account ac = new account();
        ac.Name = '12534585';
        ac.Is_Primary_Address__c=TRUE;
        ac.Is_Primary_Physical_Address__c=TRUE;
        ac.Is_Primary_Mailing_Address__c=TRUE;
         Insert ac;
        
        Test.startTest();
        
        Contact ct = ContactInsert();
        ct.Primary_Address__c=ac.id;
        ct.Primary_Physical_Address__c=ac.id;
         ct.Primary_Mailing_Address__c=ac.id; 
        update ct;
        
        Test.stopTest();
      }
     public static Contact ContactInsert()
    {
        //create an Contact
        Contact ct = new Contact();
        ct.LastName = 'test';
        ct.Phone = '1234567890';
        ct.MobilePhone = '5105489586';
        ct.Email= 'alpha@chello.be';
        insert ct;
        return ct;
    }
  }
And here is the Trigger i wrote for this test class is
trigger UpdatePrimaryAddress on Account (after insert, after update) {
    List <String> ContactIds= new List<String>();
     For(Account ac : trigger.new)
     {
       ContactIds.add(ac.Contact__c);
     }
     List<Contact>Contacts = [Select Id, Name, Primary_Address__c, Primary_Mailing_Address__c, Primary_Physical_Address__c From Contact Where id IN:ContactIds];
     Map<String,Contact>ContactMap = New Map<String,Contact>(); 
     For(Contact ct : Contacts)
     {
       ContactMap.Put(ct.id, ct);
     }
     List<Contact> ConUpdates = New List<Contact>();
     For(Account ac : Trigger.new)
     {
       system.debug('Entered for loop');
        if(ac.Is_Primary_Address__c==TRUE && ac.Is_Primary_Physical_Address__c==TRUE
                        && ac.Is_Primary_Mailing_address__c==TRUE)
        {
         Contact con = ContactMap.get(ac.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Address__c=ac.id;
                con.Primary_Physical_Address__c=ac.id;
                con.Primary_Mailing_Address__c=ac.id;
                ConUpdates.add(con);
         }
       else if(ac.Is_Primary_Address__c==TRUE && ac.Is_Primary_Physical_Address__c==TRUE)
        {
         Contact con = ContactMap.get(ac.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Address__c=ac.id;
                con.Primary_Physical_Address__c=ac.id;
                ConUpdates.add(con);
         }
       else if(ac.Is_Primary_Physical_Address__c==TRUE && ac.Is_Primary_Mailing_Address__c==TRUE)                        
        {
         Contact con = ContactMap.get(ac.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Physical_Address__c=ac.id;
                con.Primary_Mailing_Address__c=ac.id;
                ConUpdates.add(con);
         }
       else if(ac.Is_Primary_Address__c==TRUE && ac.Is_Primary_Mailing_Address__c==TRUE)
         {
           Contact con = ContactMap.get(ac.Contact__c);
                 
               system.debug(con.Id);
               con.Primary_Address__c=ac.id;
               con.Primary_Mailing_Address__c=ac.id;
               ConUpdates.add(con);
         }
     else if(ac.Is_Primary_Address__c==TRUE)
        {
         Contact con = ContactMap.get(ac.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Address__c=ac.id;
                ConUpdates.add(con);
         }
     else if(ac.Is_Primary_Physical_Address__c==TRUE)
        {
         Contact con = ContactMap.get(ac.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Physical_Address__c=ac.id;
                ConUpdates.add(con);
         }
        else if(ac.Is_Primary_Mailing_Address__c==TRUE)
        {
         Contact con = ContactMap.get(ac.Contact__c);
                
                system.debug(con.Id);
                con.Primary_Mailing_Address__c=ac.id;
                ConUpdates.add(con);
         }
      }
     
   update ConUpdates; 
   }
Thanks in Advance.

 

forecast_is_cloudyforecast_is_cloudy

I think that you issue is that you're not checking for null values in the trigger code. For e.g. after every ' Contact con = ContactMap.get(ac.Contact__c);' call, you should check to make sure that the con variable isn't null before trying to access fields on that Contact record. My guess is that the code is throwing a Null Pointer Exception somewhere in your Trigger code.

Also, I don't see you setting the parent Account Id (which is a required field) of the test Contact record that you insert in your test class. That needs to be fixed as well.

john2john2

thanks for the reply,

 

can you please suggest me some sample code..

 

thanks again,