+ Start a Discussion
Rohit Singh 136Rohit Singh 136 

Test class code covarge in not 75% for batch apex on contact update

Hi, I have to update contacts deaily if a custom number field call_interval_count with number 1 if call_interval_c !=  null. 
My batch is below:
global class updatecontactworkflow implements Database.Batchable<SObject>, Database.Stateful{
             
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'Select Id, Call_Interval__c From contact WHERE Call_Interval__c != Null';
        system.debug(query);
        return Database.getQueryLocator(query);
    }
     
    global void execute(Database.BatchableContext BC, List<SObject> scope){
        for(contact obj : (contact[]) scope){
            if(obj.Call_Interval__c != Null){
               obj.Call_Alert_Count__c = 1;
              } else 
               obj.Call_Alert_Count__c = 1;
            }
        system.debug('list to be updated size  :: ' + scope.size());
        if(!scope.isEmpty())
        {
              update scope;
         }
     }

    global void finish(Database.BatchableContext BC){
    }
}

and test class is :
@isTest             
private class updatecontactworkflowTest 
{
    static testmethod void testbatch() 
    {
        Account acc = new Account(Name='Test Account');
        insert acc;
              
        List<contact> cons = new List<contact>();
        for (integer i=0; i<50; i++)
        {
            contact con = new contact();
            con.AccountId=acc.Id;
            con.Name='My contact'+i;
            con.Level__c ='C Level';
            con.Functional_Role__c = 'Opeartion';
            con.Title = 'CFO';
			con.Phone = '+441569172306';
			con.Email = 'na@na.com';
			con.Call_Alert_Count__c = '1';
			
            //con.LastActivityDate = System.today()+15;
            cons.add(con);
        }
        insert cons;
        
       Test.startTest();
           updatecontactworkflow  obj = new  updatecontactworkflow ();
           Database.executeBatch(obj, 200);
       Test.stopTest();

    }
}

Code Coverage is 46% (6/13). Please assist :)
Best Answer chosen by Rohit Singh 136
Ankur Saini 9Ankur Saini 9
Hi Rohit,

You are using "con.Name='My contact'+i;" in your test class which is not valid because this field is not writable.
use "con.LastName='My contact'+i;"  .

try below test class.

 
@isTest             
public class updatecontactworkflow_Test {
    static testmethod void testbatch() 
    {
        Account acc = new Account(Name='Test Account');
        insert acc;
              
        List<contact> cons = new List<contact>();
        for (integer i=0; i<50; i++)
        {
            contact con = new contact();
            con.AccountId=acc.Id;
            con.LastName='My contact'+i;
			con.Call_Alert_Count__c = 0;
            con.Call_Interval__c='ABC';			
            cons.add(con);
        }
        insert cons;
        
       Test.startTest();
           updatecontactworkflow  obj = new  updatecontactworkflow ();
           Database.executeBatch(obj, 200);
       Test.stopTest();

    }
}

Thanks,
Ankur Saini
http://mirketa.com

All Answers

Rohit Singh 136Rohit Singh 136
User-added image

above is code coverage screen shot when i checked in developer console.
Ankur Saini 9Ankur Saini 9
Hi Rohit,

You are using "con.Name='My contact'+i;" in your test class which is not valid because this field is not writable.
use "con.LastName='My contact'+i;"  .

try below test class.

 
@isTest             
public class updatecontactworkflow_Test {
    static testmethod void testbatch() 
    {
        Account acc = new Account(Name='Test Account');
        insert acc;
              
        List<contact> cons = new List<contact>();
        for (integer i=0; i<50; i++)
        {
            contact con = new contact();
            con.AccountId=acc.Id;
            con.LastName='My contact'+i;
			con.Call_Alert_Count__c = 0;
            con.Call_Interval__c='ABC';			
            cons.add(con);
        }
        insert cons;
        
       Test.startTest();
           updatecontactworkflow  obj = new  updatecontactworkflow ();
           Database.executeBatch(obj, 200);
       Test.stopTest();

    }
}

Thanks,
Ankur Saini
http://mirketa.com
This was selected as the best answer
Rohit Singh 136Rohit Singh 136
Hi Ankur, 
Thank you for your reply. I did the same but still code coverage in 46%. 
 
Ankur Saini 9Ankur Saini 9
Hi 

Could you please paste your latest code (both class and test class) here ?

Thanks
Ankur Saini
Arvind KumarArvind Kumar
Hi Rohit,

I have cross-checked your batch class & latest updated test class through Ankit.

Test class is giving 90% code coverage to the Batch class.

One thing you are updating same value in Call_Alert_Count__c field for both condition if & else.


Run once again test class.

Thanks,
Arvind Kumar