+ Start a Discussion
Abraham kumarAbraham kumar 

Test Class Coverage not exceeding 69%

HI All,

I have the below trigger and test class and with this test class i can get only 69% code coverage, can you pls help me increase code coverage for below trigger so i will be able to deploy this to production.
trigger Contactcallout on Contact (after insert, after update, before delete) {
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == '012D0000000BaFA' && c.Reg__c == TRUE)
            c.status__c='inactive';
            {
                validContacts.add(c);
                accIds.add(c.accountid);
            }   
        }
    }
    else
    {
for (contact c : Trigger.new) {
    if(c.RecordTypeId == '012D0000000BaFA' && c.Reg__c == TRUE){
    if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
                }
                }
         }   
}
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
              }
}
Test Classs:-
@istest
public class ContactcalloutTestClass{
@istest
static void contactcreate(){
Account acc = new Account();
acc.Name = 'Test';
acc.ShippingStreet = 'test';
acc.ShippingCity = 'test';        
acc.ShippingPostalCode = 'tw31aq';
insert acc;

contact c =new contact();
c.Email = 'abc@gmail.com';
c.Salutation = 'Mr';
c.FirstName = 'Abraham';
c.LastName = 'Daniel';
c.type__c = 'Agency';
c.status__c= 'Active';
insert c;

Contact c1=[Select id from contact Where Id =:c.id];
c1.firstname= 'Daniel';
c1.lastname= 'Abraham';
c1.Email = 'def@gmail.com';
c1.Status__c = 'inactive';
update c1;
           try
           { 
               Delete c1;
              // c1.Status__c = 'Inactive';
            }
           catch(DmlException e) 
           {
               System.debug('The following exception has occurred: ' + e.getMessage());
            }
               }
@istest
static void contactcreate2(){
Account acc2 = new Account();
acc2.Name = 'Test';
acc2.ShippingStreet = 'test';
acc2.ShippingCity = 'test';        
acc2.ShippingPostalCode = 'tw31aq';
insert acc2;

contact c3 =new contact();
c3.Email = 'abc@gmail.com';
c3.Salutation = 'Mr';
c3.FirstName = 'Abraham';
c3.LastName = 'Daniel';
c3.type__c = 'Agency';
c3.status__c= 'Active';
insert c3;

Contact c4=[Select id from contact Where Id =:c3.id];
c4.firstname= 'Daniel';
c4.lastname= 'Abraham';
c4.Email = 'def@gmail.com';
c4.Status__c = 'inactive';
update c4;
if(c4.email !=c3.email||c4.firstname != c3.firstname||c4.lastname != c3.lastname){
Test.setMock(WebServiceMock.class, new WebServiceMockImpl());
            }
            }
            }

I have checked in the developer console and see the lines 21 to lines 32 of the trigger are not being covered. PLease help me increase code coverage to deploy this code. 

Many Thanks 
Abraham
 
Best Answer chosen by Abraham kumar
JeffreyStevensJeffreyStevens
Okay - for line #11 - When you create your contact - you're not setting the Registered_on_ITV_Media__c field to true.  That's the reason that #11 isn't getting executed.  (so like - after line#19 of your test code - add c.Registered_on_ITV_Media__c = TRUE;)  While we're on that section of code - double check what you're wanting for that section.  When you have...

if(c.RecordTypeId == RecordType1 && c.Registered_on_ITV_Media__c == TRUE) 
c.Status__c=Inactive';

That completes the if statement.  The if statement is completed after the simi-colon.  (at least I think).  Are you wanting line 11,13,14 to all be part of the if statement?  I think as it stands - 13 & 14 part just part of the For statement started on 08.

For lines 22+  - I think that it's the same type of issue.  When you're creating the contact record - you're not setting the Reg__c field to true.

Quite often in times like this - I'll use the debug statement to verify what I'm testing on.  For example - just before line #21 - I'd do statements like this...

system.debug('RecordType1='+RecordType1);
system.debug('c.RecordTypeId='+c.RecordTypeId);
system.debug('c.Reg__c='+c.Reg__c);

then look at your log - and you'll see why line #22 is NOT getting executed. 

Good luck

All Answers

JeffreyStevensJeffreyStevens
Well - the fact that your record type ids are hard coded will pose a problem.  I guessing that if you check your logs - you're not getting to line#10, because of line#9.  The ID's will be diffrent from your sandbox to your production - unless you're in a full-sandbox.  Usually - I do a SOQL at the top of the test class to get the RecordTypeId that you need. 

so - let's say that you're Contact record type name (now - you should use develoer_name) - is 'Agency' - then I'd code it like this...

id contactAgencyRecordTypeId = [SELECT id FROM RecordType WHERE sObject = 'Contact' AND Developer_Name = 'Agency'].id;
then - in your trigger at lines #9 and #20 - instead of hardcoding the ID use the field contactAgencyRecordTypeId.

HTH
Naidu PothiniNaidu Pothini
When you create a contact record, make sure you create it with record type "012D0000000BaFA"(Don't use hard coded value, Use the example suggest above by Jeffrey Stevens) and set Reg__c value to true.

 
Abraham kumarAbraham kumar
Thank you so much jeffrey,

So changing the hard coding of the record type id will increase the code coverage am i right?... 

Many Thanks
Abraham
Abraham kumarAbraham kumar
Hi Jeffrey,

I have done the changes as below, Its just my test class is failing im getting a error msg System.StringException: Invalid id: RecordType1 at Class.ContactcalloutTestClass.contactcreate: line 20, column 1
in developername im using the actual record type name which is "commercial" in my test class...is this correct?
I initially used record type ids becuse the record type id is same  in production and sandbox.
@istest
public class ContactcalloutTestClass{
@istest
static void contactcreate(){
Id RecordType1= [SELECT Id FROM RecordType WHERE SOBJECTTYPE=:'contact' AND DeveloperName=:'Commercial'].Id;
Account acc = new Account();
acc.Name = 'Test';
acc.ShippingStreet = 'test';
acc.ShippingCity = 'test';        
acc.ShippingPostalCode = 'tw31aq';
insert acc;

contact c =new contact();
c.Email = 'abc@gmail.com';
c.Salutation = 'Mr';
c.FirstName = 'Abraham';
c.LastName = 'Daniel';
c.type__c = 'Agency';
c.status__c= 'Active';
c.RecordTypeId='RecordType1';
insert c;

Contact c1=[Select id from contact Where Id =:c.id];
c1.firstname= 'Daniel';
c1.lastname= 'Abraham';
c1.Email = 'def@gmail.com';
c1.Status__c = 'inactive';
update c1;
           try
           { 
               Delete c1;
              // c1.Status__c = 'Inactive';
            }
           catch(DmlException e) 
           {
               System.debug('The following exception has occurred: ' + e.getMessage());
            }
               }}

Many Thanks
Abraham
Abraham kumarAbraham kumar
Many Thanks!! It works now but still im getting only 70% coverage . any idea how i can make it 75
This is my trigger :-
trigger Contactcallout on Contact (after insert, after update, before delete) {
Id RecordType1= [SELECT Id FROM RecordType WHERE SOBJECTTYPE=:'Contact' AND DeveloperName=:'commercial'].Id;
Map<Id, String> m = new Map<Id, String>();
list<Contact> validContacts = new list<Contact>();
set<ID> accIds = new set<ID>();
if(Trigger.isDelete)
    {
        for (contact c : Trigger.old) 
        {
            if(c.RecordTypeId == RecordType1 && c.Registered_on_ITV_Media__c == TRUE)
            c.status__c='inactive';
            {
                validContacts.add(c);
                accIds.add(c.accountid);
            }   
        }
    }
    else
    {
for (contact c : Trigger.new) {
    if(c.RecordTypeId == RecordType1 && c.Reg__c == TRUE){
    if(Trigger.isUpdate){
        contact old = Trigger.oldMap.get(c.Id);
        if (c.Email != old.Email||c.FirstName!=old.FirstName||c.LastName!=old.LastName||c.phone!=old.phone||c.Title__c!=old.Title__c||c.status__c!=old.status__c||c.AccountID!=old.AccountID||c.Reg__c == TRUE)
         {
             validContacts.add(c);
                accIds.add(c.accountid);
         }
         }else{
         validContacts.add(c);
                accIds.add(c.accountid);
                }
                }
         }   
}
map<ID, Account> accMap;
if(!accIds.IsEmpty()) // guard condition for SOQL
    accMap = new map<ID, Account>([select name from account where id in :accIds]);

for (contact c : validContacts) {
    Account acc = accMap.get(c.AccountID);
    string accName = acc == null ? null : acc.Name;
    WebServiceCallout.sendNotification(c.Id,c.Email,c.FirstName,c.LastName,c.phone,c.Title__c,accName,c.status__c);
              }
}

Test class:-
@istest
public class ContactcalloutTestClass{
@istest
static void contactcreate(){
Id RecordType1= [SELECT Id FROM RecordType WHERE SOBJECTTypE=:'contact' AND DeveloperName=:'Commercial'].Id;
Account acc = new Account();
acc.Name = 'Test';
acc.ShippingStreet = 'test';
acc.ShippingCity = 'test';        
acc.ShippingPostalCode = 'tw31aq';
insert acc;

contact c =new contact();
c.Email = 'abc@gmail.com';
c.Salutation = 'Mr';
c.FirstName = 'Abraham';
c.LastName = 'Daniel';
c.type__c = 'Agency';
c.status__c= 'Active';
c.RecordTypeId=RecordType1;
insert c;

Contact c1=[Select id from contact Where Id =:c.id];
c1.firstname= 'Daniel';
c1.lastname= 'Abraham';
c1.Email = 'def@gmail.com';
c1.Status__c = 'inactive';
update c1;
           try
           { 
               Delete c1;
              // c1.Status__c = 'Inactive';
            }
           catch(DmlException e) 
           {
               System.debug('The following exception has occurred: ' + e.getMessage());
            }
               }
}

Many Thanks in advance
Abraham

 
JeffreyStevensJeffreyStevens
which lines are not being covered?  Are you sure your record type name is all lowercase?  You've got the right concept in your test class - create a contact record, update it, delete it.
Abraham kumarAbraham kumar
HI Jeffrey Many Thanks for your response.
The record type name is "Commercial" caps of the first letter. I changed my code also still getting 70%
Lines not being covered are 
Line no 11 and
Line no 22 to 32..

 Many Thanks
Abraham
JeffreyStevensJeffreyStevens
Okay - for line #11 - When you create your contact - you're not setting the Registered_on_ITV_Media__c field to true.  That's the reason that #11 isn't getting executed.  (so like - after line#19 of your test code - add c.Registered_on_ITV_Media__c = TRUE;)  While we're on that section of code - double check what you're wanting for that section.  When you have...

if(c.RecordTypeId == RecordType1 && c.Registered_on_ITV_Media__c == TRUE) 
c.Status__c=Inactive';

That completes the if statement.  The if statement is completed after the simi-colon.  (at least I think).  Are you wanting line 11,13,14 to all be part of the if statement?  I think as it stands - 13 & 14 part just part of the For statement started on 08.

For lines 22+  - I think that it's the same type of issue.  When you're creating the contact record - you're not setting the Reg__c field to true.

Quite often in times like this - I'll use the debug statement to verify what I'm testing on.  For example - just before line #21 - I'd do statements like this...

system.debug('RecordType1='+RecordType1);
system.debug('c.RecordTypeId='+c.RecordTypeId);
system.debug('c.Reg__c='+c.Reg__c);

then look at your log - and you'll see why line #22 is NOT getting executed. 

Good luck
This was selected as the best answer
Abraham kumarAbraham kumar
Wohoo..

Thanks soo much Jeffrey yes 
c.Registered_on_ITV_Media__c == TRUE in the test class did the trick..

Many Thanks 
Abraham