You need to sign in to do that
Don't have an account?
Rohit 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:
and test class is :
Code Coverage is 46% (6/13). Please assist :)
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 :)
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.
Thanks,
Ankur Saini
http://mirketa.com
All Answers
above is code coverage screen shot when i checked in developer console.
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.
Thanks,
Ankur Saini
http://mirketa.com
Thank you for your reply. I did the same but still code coverage in 46%.
Could you please paste your latest code (both class and test class) here ?
Thanks
Ankur Saini
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