+ Start a Discussion
mohammed Imammohammed Imam 

, I have wrote test class for above this ,it covers only 35 % code coverage ,could you make it to 100%.

Code:
global with sharing class cq_ctosBatchcreateLookup implements Database.Batchable<sObject>
{
    public Database.QueryLocator start(Database.BatchableContext bc)
    { 
        //query the campaign
       // String query = '';
        return Database.getQueryLocator([select Id,Name,cq_ctos__Unique_ID__c,cq_ctos__End_Date_Time__c  
                                         from campaign where IsActive = true AND cq_ctos__End_Date_Time__c > TODAY WITH SECURITY_ENFORCED]);
    }
    public void execute(Database.BatchableContext bc,List<sObject> campaignList)
    {
        Map<String, Id> campaignMap = new Map<String, Id>(); 
        Map<String, Id> EssentialMap = new Map<String, Id>(); 
        Map<String, Decimal> updateEssentialQuantity = new  Map<String, Decimal>();
        //getting the campaign Id and unique value
        for(sObject newsObject : campaignList){
            Campaign newcampaign = (Campaign)newsObject;
            campaignMap.put(newcampaign.cq_ctos__Unique_ID__c , newcampaign.Id);
        }
        system.debug('campaignMap=='+campaignMap);
        // getting the essential
        List<cq_ctos__Essential__c> essentialList = new List<cq_ctos__Essential__c>();
        List<String>  EssentialfieldNames = new List<String>{'Id','cq_ctos__Active__c','cq_ctos__Essential_Unique_Id__c', 'Name','cq_ctos__Unique_ID__c','cq_ctos__Quantity__c',
            'cq_ctos__Campaign_Name__c','cq_ctos__Quantity_Required__c'};
            if(SecurityUtil.checkAccessibility('cq_ctos__Essential__c',EssentialfieldNames)){
                essentialList = [SELECT Id, Name, cq_ctos__Active__c,cq_ctos__Essential_Unique_Id__c, cq_ctos__Unique_ID__c, cq_ctos__Quantity__c,
                                  cq_ctos__Campaign_Name__c, cq_ctos__Quantity_Required__c FROM cq_ctos__Essential__c 
                                  WHERE cq_ctos__Unique_ID__c IN : campaignMap.keySet() AND cq_ctos__Active__c = TRUE WITH SECURITY_ENFORCED];
            }
        else{
            throw new System.AssertException('Error: Insufficient Access');
        }
        
        for(cq_ctos__Essential__c newEssential : essentialList){
            EssentialMap.put(newEssential.cq_ctos__Essential_Unique_Id__c , newEssential.Id);
        }
        
        //updating the essential dropout 
        List<cq_ctos__Essential_Pickup__c> EssentialDropOutList = new List<    cq_ctos__Essential_Pickup__c>();
        system.debug('EssentialDropOutList=='+[SELECT Id, Name, cq_ctos__LeadId__c, cq_ctos__ContactId__c, cq_ctos__Campaign_Name__c, cq_ctos__Unique_ID__c FROM cq_ctos__Essential_Pickup__c WHERE cq_ctos__Unique_ID__c IN : campaignMap.keySet() ]);
       List<String>  EssentialDropOutfieldNames = new List<String>{'Id','cq_ctos__LeadId__c','cq_ctos__ContactId__c', 'Name','cq_ctos__Campaign_Name__c','cq_ctos__Unique_ID__c'};
            if(SecurityUtil.checkAccessibility('cq_ctos__Essential_Pickup__c',EssentialDropOutfieldNames)){
            EssentialDropOutList  = [SELECT Id, Name, cq_ctos__LeadId__c, cq_ctos__ContactId__c, cq_ctos__Campaign_Name__c, cq_ctos__Unique_ID__c FROM cq_ctos__Essential_Pickup__c WHERE cq_ctos__Unique_ID__c IN : campaignMap.keySet() AND cq_ctos__Campaign_Name__c = NULL WITH SECURITY_ENFORCED];
        }
         else{
            throw new System.AssertException('Error: Insufficient Access');
        }
        
        if(!EssentialDropOutList.isEmpty()){
            for(cq_ctos__Essential_Pickup__c essDropout : EssentialDropOutList){
                essDropout.cq_ctos__Campaign_Name__c = campaignMap.get(essDropout.cq_ctos__Unique_ID__c);
            }
            if (Schema.sObjectType.cq_ctos__Essential_Pickup__c.fields.cq_ctos__Unique_ID__c.isUpdateable()) {
                // Update contact phone number
                update EssentialDropOutList;
            }
            
        }
        //updating the donated essential
        List<cq_ctos__Donated_Essential__c> EssentialManagerList = new List<cq_ctos__Donated_Essential__c>();
        List<String>  EssentialManagerfieldNames = new List<String>{'Id','cq_ctos__Lead__c','cq_ctos__Essential_Unique_Id__c', 'Name','cq_ctos__Essential_Quantity__c','cq_ctos__Contact__c', 'cq_cs__Campaign_Name__c','cq_ctos__Unique_ID__c','cq_ctos__Essential__c'};
            if(SecurityUtil.checkAccessibility('cq_ctos__Essential_Manager__c',EssentialManagerfieldNames)){
            EssentialManagerList = [SELECT Id, Name, cq_ctos__Lead__c,cq_ctos__Essential_Unique_Id__c,cq_ctos__Essential_Quantity__c, cq_ctos__Contact__c, cq_ctos__Campaign_Name__c,cq_ctos__Unique_ID__c,cq_ctos__Essential__c FROM cq_ctos__Donated_Essential__c WHERE cq_ctos__Unique_ID__c IN : campaignMap.keySet() AND cq_ctos__Campaign_Name__c = NULL AND cq_ctos__Essential__c = NULL WITH SECURITY_ENFORCED];    
        }
         else{
            throw new System.AssertException('Error: Insufficient Access');
        }
        if(!EssentialManagerList.isEmpty()){
            for(cq_ctos__Donated_Essential__c essManager : EssentialManagerList){
                essManager.cq_ctos__Campaign_Name__c = campaignMap.get(essManager.cq_ctos__Unique_ID__c);
                essManager.cq_ctos__Essential__c =  EssentialMap.get(essManager.cq_ctos__Essential_Unique_Id__c);
                updateEssentialQuantity.put(essManager.cq_ctos__Essential_Unique_Id__c, essManager.cq_ctos__Essential_Quantity__c);
            }
            if (Schema.sObjectType.cq_ctos__Donated_Essential__c.fields.cq_ctos__Essential_Unique_Id__c.isUpdateable()) {
                // Update contact phone number
                update EssentialManagerList;
            }
        }
        //updating the essential quntity
        List<cq_ctos__Essential__c> updatingessentialList = new  List<cq_ctos__Essential__c>();
        if (Schema.sObjectType.cq_ctos__Essential__c.fields.cq_ctos__Active__c.isAccessible()){
            updatingessentialList = [SELECT Id, Name, cq_ctos__Active__c,cq_ctos__Essential_Unique_Id__c, cq_ctos__Unique_ID__c, cq_ctos__Quantity__c, cq_ctos__Campaign_Name__c, cq_ctos__Quantity_Required__c FROM cq_ctos__Essential__c WHERE cq_ctos__Essential_Unique_Id__c IN:updateEssentialQuantity.keySet() WITH SECURITY_ENFORCED];
        }
        if(!updateEssentialQuantity.isEmpty()){
            for(cq_ctos__Essential__c newEssential : updatingessentialList){
                newEssential.cq_ctos__Quantity_Required__c = updateEssentialQuantity.get(newEssential.cq_ctos__Essential_Unique_Id__c);
            }
            if (Schema.sObjectType.cq_ctos__Essential__c.fields.cq_ctos__Essential_Unique_Id__c.isUpdateable()) {
                // Update contact phone number
                update updatingessentialList;
            }
            
        }
        // creating the campaign member
        List<cq_ctos__Campaign_Member_Stage__c> cmapaignMemberStage = new  List<cq_ctos__Campaign_Member_Stage__c>();
        if (Schema.sObjectType.cq_ctos__Campaign_Member_Stage__c.fields.cq_ctos__Unique_ID__c.isAccessible()){
            cmapaignMemberStage= [SELECT Id,Name,cq_ctos__Unique_ID__c,cq_ctos__Status__c,cq_ctos__Lead__c,cq_ctos__Contact__c 
                                  FROM cq_ctos__Campaign_Member_Stage__c WHERE cq_ctos__Unique_ID__c IN : campaignMap.keySet() WITH SECURITY_ENFORCED];
            
        }
       List<CampaignMember> newCampaignMember = new  List<CampaignMember>();
        if(!cmapaignMemberStage.isEmpty()){
          
            
            for(cq_ctos__Campaign_Member_Stage__c campaignMemStage  : cmapaignMemberStage){
                CampaignMember newCmapignMem = new CampaignMember();
                newCmapignMem.CampaignId = campaignMap.get(campaignMemStage.cq_ctos__Unique_ID__c);
                if(campaignMemStage.cq_ctos__Contact__c == null){
                    newCmapignMem.LeadId = campaignMemStage.cq_ctos__Lead__c;
                }else{
                    newCmapignMem.ContactId = campaignMemStage.cq_ctos__Contact__c;
                }
                newCmapignMem.Status = campaignMemStage.cq_ctos__Status__c; 
                if(newCampaignMember.size() > 0) 
                newCampaignMember.add(newCmapignMem);
                
            }
            if (Schema.sObjectType.CampaignMember.fields.CampaignId.isCreateable() &&
                Schema.sObjectType.CampaignMember.fields.LeadId.isCreateable() &&
                Schema.sObjectType.CampaignMember.fields.ContactId.isCreateable() &&
                Schema.sObjectType.CampaignMember.fields.Status.isCreateable() ) {
                    // Create new CampaignMember 
                                       
                    insert newCampaignMember;
                 }
            set<Id> camMemstageId = new set<Id>();
            for(cq_ctos__Campaign_Member_Stage__c campaignMemStage  : cmapaignMemberStage){
                camMemstageId.add(campaignMemStage.Id);
            }
            List<cq_ctos__Campaign_Member_Stage__c> deletcampaignMemStage = new  List<cq_ctos__Campaign_Member_Stage__c> ();
            if (Schema.sObjectType.cq_ctos__Campaign_Member_Stage__c.fields.cq_ctos__Unique_ID__c.isAccessible()){
                deletcampaignMemStage= [SELECT Id,cq_ctos__Unique_ID__c,cq_ctos__Status__c,cq_ctos__Lead__c,cq_ctos__Contact__c
                                        FROM cq_ctos__Campaign_Member_Stage__c WHERE Id IN : camMemstageId WITH SECURITY_ENFORCED];
                
            }
            Integer start = System.Now().millisecond();
            while(System.Now().millisecond()< start+10){ 
            }
            if (cq_ctos__Campaign_Member_Stage__c.sObjectType.getDescribe().isDeletable()) {
                // Delete contact
                delete deletcampaignMemStage;
            }
            
        }
        
    }
    public  void finish(Database.BatchableContext bc)
    {
        system.debug('finish');
        if(!test.isRunningTest()) {
            system.scheduleBatch(New cq_ctosBatchcreateLookup(),'Run again in 2',2);
        }
    }   
    
}

Test Class:

@isTest(SeeAllData=false)
public class cq_ctosBatchcreateLookupTest {
    
    static testMethod void myTest(){
        Campaign campaign=new campaign();
        campaign.Name='Test Campaign';
        campaign.cq_ctos__End_Date_Time__c=system.now().adddays(1);
        campaign.IsActive=true;
        insert campaign;
        
        string campaignUniqueId=[SELECT id,cq_ctos__Unique_ID__c from Campaign WHERE id =:campaign.id].cq_ctos__Unique_ID__c;
        
        Lead newlead1 = new Lead();
        newlead1.FirstName = 'Test';
        newlead1.LastName = 'Lead';
        newlead1.Email = 'testlead@wed.net';
        newlead1.Company = 'clouddd';
        newlead1.cq_ctos__CampaignId__c = campaign.Id;
        newlead1.Phone = '484842252255';
        insert newlead1; 
        
        cq_ctos__Essential__c newEssential = new cq_ctos__Essential__c();
        newEssential.Name= 'masfk';
        newEssential.cq_ctos__Active__c=true;
        newEssential.cq_ctos__Quantity__c = 44;
        newEssential.cq_ctos__Campaign_Name__c = campaign.Id;
        insert newEssential;
        
       cq_ctos__Essential_Pickup__c newdropout = new cq_ctos__Essential_Pickup__c();
        newdropout.cq_ctos__LeadId__c = newlead1.Id;
        newdropout.cq_ctos__Unique_ID__c = campaignUniqueId;
        newdropout.cq_ctos__City__c = 'ndecff';
        newdropout.cq_ctos__Pickup_Date_Time__c = datetime.newInstance(2022, 6, 23, 15, 23, 30);
        newdropout.cq_ctos__Postal_Code__c = '560100';
        newdropout.cq_ctos__State__c='xdfdfdf';
        newdropout.cq_ctos__Street__c = 'fafcdcafa';
        insert newdropout; 
        system.debug(' newdropout.cq_ctos__Unique_ID__c=='+ newdropout.cq_ctos__Unique_ID__c);
          cq_ctos__Donated_Essential__c newdonatedEss = new cq_ctos__Donated_Essential__c();
        newdonatedEss.cq_ctos__Unique_ID__c = campaignUniqueId;
        newdonatedEss.cq_ctos__Quantity__c = 4;
        newdonatedEss.cq_ctos__Name__c =  newEssential.Name;
        newdonatedEss.cq_ctos__Lead__c = newlead1.Id;
        newdonatedEss.cq_ctos__Essential_Quantity__c = newEssential.cq_ctos__Quantity__c- 4;
        newdonatedEss.cq_ctos__Essential_Unique_Id__c = newEssential.cq_ctos__Essential_Unique_Id__c;
        insert newdonatedEss;
        
        cq_ctos__Campaign_Member_Stage__c newCampaignStage = new cq_ctos__Campaign_Member_Stage__c();
        newCampaignStage.Name = newCampaignStage.Id;
        newCampaignStage.cq_ctos__Unique_ID__c =campaignUniqueId;
        newCampaignStage.cq_ctos__Status__c = 'Sent';
        newCampaignStage.cq_ctos__Lead__c  = newlead1.Id;
        insert newCampaignStage; 
        
        CampaignMember newCampaignMember = new  CampaignMember();
        newCampaignMember.LeadId = newlead1.Id;
        newCampaignMember.Status = 'Sent';
        newCampaignMember.cq_ctos__Unique_ID__c = newCampaignStage.cq_ctos__Unique_ID__c;
        newCampaignMember.ContactId = newCampaignStage.cq_ctos__Contact__c ;
        newCampaignMember.CampaignId = newlead1.cq_ctos__CampaignId__c;
        
        insert newCampaignMember; 
        
        
        cq_ctosBatchcreateLookup a = new cq_ctosBatchcreateLookup();
        test.startTest();
        ID id = Database.executeBatch(a);
        System.assertEquals(newCampaignStage.cq_ctos__Lead__c,newlead1.Id);
        test.stopTest();
    }
}