function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex MezaAlex Meza 

Batch class test coverage

I am trying to deploy one of my batch classes into my production enviroment but I currnelty was only able to write a batch test class that has 29 percent coverage. I was wondering if you guys could help me get my coverage up to 100% so that I can deploy to production. 

Apex Batch Class
global class UpdateContactsVoter implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Select ID,FirstName,LastName,MailingPostalCode from contact where contact.voter_file_id__c = null';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext info, List<contact> scope){
        Set<String> set_Str = new Set<string>();
                                Map<String,Voter_File_TX__c> mp_VoterFile;

        for(Voter_File_TX__c VoterFile : [Select ID,First_Name__c, Last_Name__c,Zipcode__c, Contact__c From Voter_File_TX__c] ){
                                                if(mp_VoterFile==null){
                                                                mp_VoterFile = new Map<String,Voter_File_TX__c>();
                                                }
                                                mp_VoterFile.put(VoterFile.First_Name__c +''+VoterFile.Last_Name__c +''+VoterFile.Zipcode__c,VoterFile);
                                }
                                for(Contact ContactList : scope){
                                                if(mp_VoterFile!=null && mp_VoterFile.containsKey(ContactList.FirstName+''+ ContactList.LastName+''+ ContactList.MailingPostalCode))
            
            {
                                                                mp_VoterFile.get(ContactList.FirstName +''+ ContactList.LastName +''+ ContactList.MailingPostalCode).Contact__c = ContactList.id;
                                                }
                                }
                                if(mp_VoterFile!=null && mp_VoterFile.values()!=null){
                                                update mp_VoterFile.values();
                                }
    }
    
    global void finish(Database.BatchableContext info){
        
    }
}

Test Class
@isTest
public class TestClassUpdateContacts
{
static testMethod void testMethod1()
{
List lstContact= new List();
List lstVoterfile = new List();
if(contact.voter_file_id__c == null && voter_file_tx__c.contact__c == null)
{
Contact cont = new Contact();
cont.FirstName ='Test';
cont.LastName = 'Test';
cont.id = 'Test';
cont.MailingPostalCode = 'test';

insert cont;

Voter_File_TX__c vf = new Voter_File_TX__c();
vf.First_Name__c ='Test';
vf.Last_Name__c ='Test';
vf.RNC_ID__c = 'Test';
vf.Zipcode__c = 'Test';
vf.First_Name__c = cont.FirstName ;
vf.Last_Name__c = cont.LastName ;
vf.Zipcode__c = cont.MailingPostalCode ;
vf.Contact__c = cont.Id ;

insert vf;

cont.FirstName = vf.First_Name__c ;
cont.LastName = vf.Last_Name__c ;
cont.MailingPostalCode = vf.Zipcode__c;
cont.RNC_ID__c = vf.RNC_ID__c ;

update cont;
}
else {
contact cont1 = new Contact();
Voter_File_TX__c vf1 = new Voter_File_TX__c();
cont1.Voter_File_ID__c = null;
vf1.Contact__c = null;

}
insert lstContact;
update lstContact;

Test.startTest();

UpdateContactsVoter obj = new UpdateContactsVoter();
DataBase.executeBatch(obj);

Test.stopTest();
}
}

 
Best Answer chosen by Alex Meza
Ajay K DubediAjay K Dubedi
Hi Alex,
In this class code coverage is 93% and you can easily deploy your production.
Test Class:-
@isTest
public class TestClassUpdateContacts{
static testMethod void testMethod1(){
       //List<Contact> lstContact= new List<Contact>();
       //List<Voter_File_TX__c> lstVoterfile = new List<Voter_File_TX__c>();
Contact cont = new Contact();
       cont.FirstName ='Test';
       cont.LastName = 'Test';
       cont.MailingPostalCode = 'test';
Insert cont;
Voter_File_TX__c vf = new Voter_File_TX__c();
       vf.First_Name__c ='Test';
       vf.Last_Name__c ='Test';
       vf.Zipcode__c = 'Test';
vf.Contact__c = cont.Id ;
vf.RNC_ID__c = 'Test';
Insert vf;
/*
       vf.RNC_ID__c = 'Test';
       vf.First_Name__c = cont.FirstName ;
       vf.Last_Name__c = cont.LastName ;
       vf.Zipcode__c = cont.MailingPostalCode ;
       vf.Contact__c = cont.Id ;
       insert vf;*/
       cont.voter_file_id__c = null;
Update cont;
Contact cont1 = new Contact();
Voter_File_TX__c vf1 = new Voter_File_TX__c();
       cont1.voter_file_id__c = null;
       vf1.Contact__c = null;
//insert lstContact;
// update lstContact;
    Test.startTest();
       UpdateContactsVoter obj = new UpdateContactsVoter();
       DataBase.executeBatch(obj);
    Test.stopTest();
   }
}
Output:-

User-added image

Hope it will help you
Regards,
Ajay