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
sami31sami31 

Test class batch apex

HI I created a batch apex but my test class is not covering the entire code, not sure where i am going wrong. Looks like query in start method is not returning any test records which i created. Please shed some light on where i am going wrong.

Batch class:
global class updatePrimaryZipTerritory implements Database.Batchable<sobject>{
    public String query='select id,Zip_vod__c,INDV_CL_Territory__c,Country_vod__c from Address_vod__c where Primary_vod__c=TRUE AND Country_vod__c=\'us\'';
    global Database.QueryLocator start(Database.BatchableContext BC){
        List<Address_vod__c> ad=[select id,Zip_vod__c,INDV_CL_Territory__c,Country_vod__c from Address_vod__c where Primary_vod__c=TRUE AND Country_vod__c='us'];
        System.debug('****'+ad.size());
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sobject> scope){
        List<Address_vod__c> addrList=(List<Address_vod__c>)scope;
        System.debug('Query List'+addrList);
        List<Address_vod__c> updateTerr=new List<Address_vod__c>();
        Set<String> zipIds=new Set<String>();
        for(Address_vod__c ad:addrList){
            if(ad.Zip_vod__c!='')
            zipIds.add(ad.Zip_vod__c);
        }
        System.debug('Zip Codes'+zipIds+'Size'+zipIds.size());
           Map<String,String> zipTerr=new Map<String,String>();
        for(INDV_Zip_To_Territory__c zip:[select Name,INDV_Territory_Id__c from INDV_Zip_To_Territory__c where Name in: zipIds]){
                        zipTerr.put(zip.Name,zip.INDV_Territory_Id__c);
        }
        System.debug('Map of Zip Terr'+zipTerr+'Size'+zipTerr.size());
        if(!zipTerr.isEmpty()){
        for(Address_vod__c add:addrList){
            if(zipTerr.containsKey(add.Zip_vod__c)){
                if(String.isBlank(add.INDV_CL_Territory__c) || zipTerr.get(add.Zip_vod__c)!=add.INDV_CL_Territory__c){
                    add.INDV_CL_Territory__c=zipTerr.get(add.Zip_vod__c);
                    updateTerr.add(add);
                }
            }                
        }
        }    
        System.debug('Update Territory List'+updateTerr+updateTerr.size());
        if(!updateTerr.isEmpty()){
            update updateTerr;
        }
    }
    global void finish(Database.BatchableContext BC){
   
    }
}

Test Class:
@isTest
private class updatePrimaryZipTerritoryTest {
   static testmethod void updateTerritoryTestMethod(){
        List<INDV_Zip_To_Territory__c> zip_terr=new List<INDV_Zip_To_Territory__c>();
        List<Address_vod__c> addrList=new List<Address_vod__c>();
           Account acc=new Account(lastName='Test Batch');
        insert acc;
       List<String> zip=new List<String>{'00680', '00681', '00682', '00683', '00684', '00685', '00686', '00687', '00688', '00689'};
        for(integer i=0;i<10;i++){
            addrList.add(new Address_vod__c(Account_vod__c=acc.id,Name='test'+i,Primary_vod__c=TRUE,Country_vod__c='us',city_vod__c='test',State_vod__c='VA',Zip_vod__c=zip[i]));
            zip_terr.add(new INDV_Zip_To_Territory__c(Name=zip[i],INDV_Territory_Id__c='29999999'));
        }
        insert addrList;
         insert zip_terr;
        
        system.debug('Before Address+++'+addrList);    
        Test.startTest();
        updatePrimaryZipTerritory cb=new updatePrimaryZipTerritory();
        Id batchId = Database.executeBatch(cb);
        Test.stopTest();

        system.debug('After Address+++'+addrList);
        System.assertEquals(7,[select count() from Address_vod__c where INDV_CL_Territory__c!='']);
    }
}

Thanks
 
N.M. SharmaN.M. Sharma
Hi Sameer,
 
global class updatePrimaryZipTerritory implements Database.Batchable<sobject>{
    public String query = 'select id, Zip_vod__c, INDV_CL_Territory__c, Country_vod__c from Address_vod__c where Primary_vod__c=TRUE AND Country_vod__c=\'us\'';
    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<sobject> scope){
        List<Address_vod__c> addrList = (List<Address_vod__c>)scope;
        System.debug('Query List'+addrList);
        List<Address_vod__c> updateTerr = new List<Address_vod__c>();
        Set<String> zipIds = new Set<String>();
        for(Address_vod__c ad : addrList){
            if(ad.Zip_vod__c!='')
            zipIds.add(ad.Zip_vod__c);
        }
        System.debug('Zip Codes' + zipIds + 'Size'+zipIds.size());
        Map<String,String> zipTerr=new Map<String,String>();
        for(INDV_Zip_To_Territory__c zip:[select Name,INDV_Territory_Id__c from INDV_Zip_To_Territory__c where Name in: zipIds]){
                        zipTerr.put(zip.Name,zip.INDV_Territory_Id__c);
        }
        System.debug('Map of Zip Terr'+zipTerr+'Size'+zipTerr.size());
        if(!zipTerr.isEmpty()){
			for(Address_vod__c add:addrList){
				if(zipTerr.containsKey(add.Zip_vod__c)){
					if(String.isBlank(add.INDV_CL_Territory__c) || zipTerr.get(add.Zip_vod__c)!=add.INDV_CL_Territory__c){
						add.INDV_CL_Territory__c=zipTerr.get(add.Zip_vod__c);
						updateTerr.add(add);
					}
				}                
			}
        }    
        System.debug('Update Territory List'+updateTerr+updateTerr.size());
        if(!updateTerr.isEmpty()){
            update updateTerr;
        }
    }
    global void finish(Database.BatchableContext BC){
   
    }
}

Your testClass is ok i have found some mistake in your main code i have update this code now please check and me know if it is ok.

Thanks
sami31sami31
Hi 
What changes you made in the code?
Thanks,
sami31sami31
Hi Sharma,

If my test class is ok then why it is not covering start or execute methods. I chekced debug logs and the query in start method is returning 0 records. Please help me if i am doing somethin wron here.
Thanks
N.M. SharmaN.M. Sharma
Hi Sameer,

According to your code it seems like every thing is ok.
If you are comfortable you can provide me your credential if not so we make a Skype call so i will make some trouble shooting on it.

My gmail id: nitin0510sh@gmail.com
Skype: nitin.sharma0510

Thanks