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
santhiya duraisanthiya durai 

execute method is not covering

I have created a test class for batch class. But Execute method is not covering even single line. Could anypne help me on this?

Class:
public class UPRSync implements Database.Batchable<sObject>,Database.stateful{
    public string AssortmentId;
    public String InputQuery;
    public UPRSync(String Query1){
        AssortmentId='NA';
        InputQuery=Query1;
    }
    public UPRSync(Id AssortId){
        AssortmentId='NAS';
        AssortmentId=AssortId;
    }
    public Database.QueryLocator start(Database.BatchableContext BC){
        Datetime hourBack = Datetime.now().addMinutes(-60);
        String Query;
        system.debug(AssortmentId);
        if(AssortmentId=='NA'){
            Query=InputQuery;
        }else{
            system.debug('AssortmentId==>'+AssortmentId);
            Query='select ICIX_V1__Product1__c,ICIX_V1__Product2__c from ICIX_V1__PP_Relationship__c where ICIX_V1__Product2__c !=null and  ICIX_V1__Type__c=\'component\' and ICIX_V1__Product1__c=:AssortmentId';
        }
        return Database.getQueryLocator(Query);
    }
    public void execute(Database.BatchableContext BC, List<ICIX_V1__PP_Relationship__c> scope){
        //Assortment or Solid Ids
        Set<Id> AsstId=new set<Id>();
        for(ICIX_V1__PP_Relationship__c pp : Scope){
            AsstId.add(pp.ICIX_V1__Product1__c);
        }
        AsstId.remove(null);
        List<ICIX_V1__ICIX_Product__c> Scope1=[Select Id,Name from ICIX_V1__ICIX_Product__c where Id in : AsstId];
        set<Id> AllIds=new set<Id>();
        map<Id,List<Id>> MpPP=new map<Id,List<Id>>();
        for(ICIX_V1__PP_Relationship__c pp : [select ICIX_V1__Product1__c,ICIX_V1__Product2__c from ICIX_V1__PP_Relationship__c where ICIX_V1__Product2__c !=null and ICIX_V1__Product1__c in: AsstId and ICIX_V1__Type__c='component'] ){
            AllIds.add(pp.ICIX_V1__Product1__c);
            AllIds.add(pp.ICIX_V1__Product2__c);
            //Creating a map of Assortment/Solid and its Retail items
            if(MpPP.containskey(pp.ICIX_V1__Product1__c)){
                MpPP.get(pp.ICIX_V1__Product1__c).add(pp.ICIX_V1__Product2__c);
            }else{
                MpPP.put(pp.ICIX_V1__Product1__c, new list<Id> { pp.ICIX_V1__Product2__c });
            }
        }
        map<Id,ICIX_V1__ICIX_Product__c> Mpprod=new map<Id,ICIX_V1__ICIX_Product__c>([Select id,name from ICIX_V1__ICIX_Product__c where Id in : AllIds]);
        Id InternalAccId=[select Id from Account where ICIX_V1__Internal__c=true].Id;
        AllIds.remove(null);
        //Map of Product and 
        map<Id,List<ICIX_V1__UP_Relationship__c>> MpUprs=new map<Id,List<ICIX_V1__UP_Relationship__c>>();
        List<ICIX_V1__UP_Relationship__c> Uprlst=[select Id,ICIX_V1__Related_Account__c,ICIX_V1__Product__c from ICIX_V1__UP_Relationship__c where ICIX_V1__Product__c in : AllIds ];
        //Capturing the Product and its upr in a map..
        for(ICIX_V1__UP_Relationship__c upr : Uprlst){
            if(MpUprs.containskey(upr.ICIX_V1__Product__c)){
                MpUprs.get(upr.ICIX_V1__Product__c).add(upr);
            }else{
                MpUprs.put(upr.ICIX_V1__Product__c, new list<ICIX_V1__UP_Relationship__c> { upr });
            }
        }
        //List to Insert..
        List<ICIX_V1__UP_Relationship__c> UprlsttoUpdate=new List<ICIX_V1__UP_Relationship__c>();
        List<ICIX_V1__UP_Relationship__c> UprlsttoUpdateset=new List<ICIX_V1__UP_Relationship__c>();
        List<ICIX_V1__UP_Relationship__c> UprlsttoInsertset=new List<ICIX_V1__UP_Relationship__c>();
        for(ICIX_V1__ICIX_Product__c Prd : scope1){
            if(MpPP.containskey(Prd.Id)){
                List<Id> RetailIds=MpPP.get(Prd.Id);
                for(Id Rid: RetailIds){
                    list<ICIX_V1__UP_Relationship__c> UpAsst=MpUprs.get(Prd.Id);
                    list<ICIX_V1__UP_Relationship__c> REuprs=MpUprs.get(Rid);
                    system.debug('Retail Item Ids==>'+Rid);
                    system.debug('Upr Ids==>'+UpAsst.Size());
                    system.debug('Retail Upr Ids==>'+REuprs.size());
                    set<Id> TradingPartners=new set<Id>();
                    Id BlankUpr; 
                    Boolean populateUpr=false;
                    for(ICIX_V1__UP_Relationship__c upr : REuprs){
                        if(upr.ICIX_V1__Related_Account__c==null){
                            BlankUpr=upr.Id;
                            populateUpr=true;
                        }else{
                            TradingPartners.add(upr.ICIX_V1__Related_Account__c);  
                        }
                    }
                    system.debug('Trading Partners size==>'+TradingPartners.size());
                    for(ICIX_V1__UP_Relationship__c uprAt : UpAsst){
                        if(populateUpr){
                            ICIX_V1__UP_Relationship__c upt=new ICIX_V1__UP_Relationship__c();
                            upt.Id=BlankUpr;
                            upt.ICIX_V1__Related_Account__c=uprAt.ICIX_V1__Related_Account__c;
                            UprlsttoUpdateset.add(upt);
                            populateUpr=false;
                        }
                        if(!populateUpr){
                            if(!TradingPartners.contains(uprAt.ICIX_V1__Related_Account__c)){
                                system.debug('Inside');
                                ICIX_V1__UP_Relationship__c uprIn=new ICIX_V1__UP_Relationship__c();
                                uprIn.Name=Mpprod.get(Rid).Name;
                                uprIn.ICIX_V1__Related_Account__c=uprAt.ICIX_V1__Related_Account__c;
                                uprIn.ICIX_V1__Product__c=Rid;
                                uprIn.ICIX_V1__Status__c='Active';
                                uprIn.ICIX_V1__Type__c='Buy';
                                uprIn.ICIX_V1__UBE__c=InternalAccId;
                                
                                UprlsttoInsertset.add(uprIn); 
                                
                            } 
                        }
                    }
                }
            }
        }
        if(!UprlsttoUpdateset.Isempty()){
            RecursivePreventer.recursiveFlag=false;
            for(ICIX_V1__UP_Relationship__c uprt : UprlsttoUpdateset){
                if(uprt.ICIX_V1__Related_Account__c!=null){
                    UprlsttoUpdate.add(uprt);
                }
            }
            database.update(UprlsttoUpdate,false);
        }
        if(!UprlsttoInsertset.Isempty()){
            
            RecursivePreventer.recursiveFlag=false;
            List<ICIX_V1__UP_Relationship__c> UprlsttoInsert=new List<ICIX_V1__UP_Relationship__c>();
            for(ICIX_V1__UP_Relationship__c uprt : UprlsttoInsertset){
                if(uprt.ICIX_V1__Related_Account__c!=null){
                    UprlsttoInsert.add(uprt);
                }
            }
            
            database.insert(UprlsttoInsert,false);
            //Logic for Querying the Attributes and Non Universal Id..
            map<Id,List<Id>> MpInsert=new map<Id,List<Id>>();
            set<Id> NewUprIds=new set<Id>();
            Map<Id, Id> uprIdVsProductIdMap = new Map<Id, Id>();
            for(ICIX_V1__UP_Relationship__c upc : UprlsttoInsert){
                NewUprIds.add(upc.Id);
                uprIdVsProductIdMap.put(upc.Id, upc.ICIX_V1__Product__c);
                if(MpInsert.containskey(upc.ICIX_V1__Product__c)){
                    MpInsert.get(upc.ICIX_V1__Product__c).add(upc.Id);
                }else{
                    MpInsert.put(upc.ICIX_V1__Product__c, new list<Id> { upc.Id });
                }
            }
            NewUprIds.remove(null);
            map<Id,Id> mpUprF=new map<Id,Id>();
            for(ICIX_V1__UP_Relationship__c upr :[select id,ICIX_V1__Product__c,ICIX_V1__Related_Account__c from ICIX_V1__UP_Relationship__c where ICIX_V1__Product__c in : AllIds and ICIX_V1__Related_Account__c!=null and  Id not in : NewUprIds] ){
                mpUprF.put(upr.ICIX_V1__Product__c,upr.Id);
            }
            List<ICIX_V1__UP_Relationship_Attribute__c> uprlsts=new list<ICIX_V1__UP_Relationship_Attribute__c>();
            list<ICIX_V1__Product_Non_Universal_Id__c> ProdNonId=new List<ICIX_V1__Product_Non_Universal_Id__c>();
            map<string,ICIX_V1__UP_Relationship_Attribute__c> MpAttr=new map<string,ICIX_V1__UP_Relationship_Attribute__c>();
            map<string,ICIX_V1__Product_Non_Universal_Id__c> MpNon=new map<string,ICIX_V1__Product_Non_Universal_Id__c>();
            List<ICIX_V1__UP_Relationship_Attribute__c> UprAlst=[select id,ICIX_V1__Attribute__c,ICIX_V1__Attribute_Value__c,ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c,ICIX_V1__UP_Relationship__c from ICIX_V1__UP_Relationship_Attribute__c where ICIX_V1__UP_Relationship__c in : mpUprF.values() and ICIX_V1__Attribute__r.ICIX_V1__Type__c='Attribute'];
            for(ICIX_V1__UP_Relationship_Attribute__c upra: UprAlst){
                MpAttr.put(upra.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c+'-'+upra.ICIX_V1__Attribute__c,upra);
            }
            List<ICIX_V1__Product_Non_Universal_Id__c> ProdNonLst=[select id,ICIX_V1__Id_Type__c,ICIX_V1__Id_Value__c,ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c from ICIX_V1__Product_Non_Universal_Id__c where ICIX_V1__UP_Relationship__c in : mpUprF.values()];
            for(ICIX_V1__Product_Non_Universal_Id__c upra: ProdNonLst){
                MpNon.put(upra.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c+'-'+upra.ICIX_V1__Id_Type__c,upra);
            }
            Hasbro_UpRelationshipTriggerHelper.FirstRun=false;
            for(ICIX_V1__UP_Relationship_Attribute__c uprt: MpAttr.values()){
                if(MpInsert.containskey(uprt.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
                    List<Id> Uprids=MpInsert.get(uprt.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c);
                    for(Id Ids : Uprids){
                        if(Ids!=null){
                            system.debug('====Upr ids=='+ids);
                            ICIX_V1__UP_Relationship_Attribute__c Upra=new ICIX_V1__UP_Relationship_Attribute__c();
                            //Upra.name=ids;
                            Upra.ICIX_V1__Attribute_Value__c=uprt.ICIX_V1__Attribute_Value__c;
                            Upra.ICIX_V1__Attribute__c=uprt.ICIX_V1__Attribute__c;
                            Upra.ICIX_V1__UP_Relationship__c=ids;
                            uprlsts.add(Upra);
                        }
                    }
                }
            }
            Insert uprlsts;//Inserting Product Attributes
            List<Id> prodNonUniIdUprIdList = new List<Id>();
            for(ICIX_V1__Product_Non_Universal_Id__c PnID : MpNon.values() ){
                if(MpInsert.containskey(PnID.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c)){
                    List<Id> Uprids=MpInsert.get(PnID.ICIX_V1__UP_Relationship__r.ICIX_V1__Product__c);
                    for(Id Ids : Uprids){
                        if(ids!=null){
                            system.debug('=======>ppnon uid'+ids);
                            ICIX_V1__Product_Non_Universal_Id__c Pid=new ICIX_V1__Product_Non_Universal_Id__c();
                            Pid.ICIX_V1__Id_Type__c=PnID.ICIX_V1__Id_Type__c;
                            pid.ICIX_V1__Id_Value__c=PnID.ICIX_V1__Id_Value__c;
                            pid.ICIX_V1__UP_Relationship__c=Ids;
                            prodNonUniIdUprIdList.add(Ids);
                            ProdNonId.add(Pid);
                        }
                    }
                }
            }
            
            
            List<ICIX_V1__ICIX_Product__c> productsList = [SELECT Id, ICIX_V1__External_Id__c FROM ICIX_V1__ICIX_Product__c WHERE Id IN: MpInsert.keySet() AND ICIX_V1__External_Id__c != null];
            Map<String, String> productIdVsExternalIdMap = new Map<String, String>();
            
            if(productsList.size()>0){
                for(ICIX_V1__ICIX_Product__c pro: productsList){
                    productIdVsExternalIdMap.put(pro.Id, pro.ICIX_V1__External_Id__c);
                    
                }
                
            }
            
            ICIX_V1__Id_Type__c idType = [SELECT Id, Name, ICIX_V1__Type__c, ICIX_V1__Unique_Id__c
                                          FROM ICIX_V1__Id_Type__c
                                          WHERE ICIX_V1__Unique_Id__c = 'ITEM_N'
                                          AND ICIX_V1__Type__c = 'NonUniversal' LIMIT 1];
            System.debug('=========idType========'+idType);
            
            if(idType != null){
                
                for(ICIX_V1__UP_Relationship__c uprId: UprlsttoInsert){
                    if(uprId.Id != null){
                    if(!prodNonUniIdUprIdList.contains(uprId.Id)){
                        if(uprIdVsProductIdMap.containsKey(uprId.Id) && productIdVsExternalIdMap.containsKey(uprIdVsProductIdMap.get(uprId.Id))){
                        
                        ICIX_V1__Product_Non_Universal_Id__c PnuId = new ICIX_V1__Product_Non_Universal_Id__c();
                        PnuId.ICIX_V1__Id_Type__c = idType.Id;
                        PnuId.ICIX_V1__Id_Value__c = productIdVsExternalIdMap.get(uprIdVsProductIdMap.get(uprId.Id));
                        PnuId.ICIX_V1__UP_Relationship__c = uprId.Id;
                        
                        ProdNonId.add(PnuId);
                        
                        
                    }
                    
                    }
                    } 
                }
                
                
                
                
            }
            
            
            if(ProdNonId.size()>0){
                
                insert ProdNonId;//Inserting Product Non Universal Ids
            }
        }
    }
    public void finish(Database.BatchableContext BC){
    }
}

Test class:
@isTest
Public class UPRSync_Test{
    @isTest Static void testUPRSync(){
        try{
        //Create TheCustom settings
        PP_Deletion_Batch__c pp_del=new PP_Deletion_Batch__c();
        pp_del.name='PPDeletionBatch';
        pp_del.Is_Processed__c=true;
        insert pp_del;
        //create Account
        list<Account> accs=Hasbro_TestDataFactory.createAccount();
        accs[0].facility_name__c=accs[0].name;
        Update accs;
        //Create Responder Account
        Account acc=new Account();
        acc.name='Hasbro Dev Stg Req';
        acc.ICIX_V1__Status__c='Active';
        acc.ICIX_V1__ICIX_ID__c='304789';
        acc.ICIX_V1__Internal__c=true;
        acc.facility_name__c='Hasbro Dev Stg Req';
        insert acc;
        //create the product
        list<ICIX_V1__ICIX_Product__c> AssortProd=Hasbro_TestDataFactory.createProduct();
        //Create RetailItem Product
        list<ICIX_V1__ICIX_Product__c> RetailProd=Hasbro_TestDataFactory.createReatilItem();
        //create the PP Relation 
        list<ICIX_V1__PP_Relationship__c> PPr =Hasbro_TestDataFactory.createPP_relationship();
        ppr[0].ICIX_V1__Product1__c=AssortProd[0].id;
        ppr[0].ICIX_V1__Product2__c=RetailProd[0].id;
        ppr[0].ICIX_V1__Type__c='component';
        Update ppr;
        //create Up Relation record
        list< ICIX_V1__UP_Relationship__c> Upr_rel=new list<ICIX_V1__UP_Relationship__c>();
        ICIX_V1__UP_Relationship__c up_realationShip=new ICIX_V1__UP_Relationship__c();
        up_realationShip.name='BLP BLACK PANTHER MOVIE';
        up_realationShip.ICIX_V1__Status__c='Active';
        up_realationShip.ICIX_V1__Type__c='Buy';
        up_realationShip.ICIX_V1__Product__c=AssortProd[0].Id;
        up_realationShip.ICIX_V1__Related_Account__c=acc.Id;
        //up_realationShip.ICIX_V1__UBE__c=acc.id;
        Upr_rel.add(up_realationShip);
        ICIX_V1__UP_Relationship__c up_realationShip1=new ICIX_V1__UP_Relationship__c();
        up_realationShip1.name='BLP BLACK PANTHER MOVIE';
        up_realationShip1.ICIX_V1__Status__c='Active';
        up_realationShip1.ICIX_V1__Type__c='Buy';
        up_realationShip1.ICIX_V1__Product__c=RetailProd[0].Id;
        up_realationShip1.ICIX_V1__Related_Account__c=acc.Id;
       Upr_rel.add(up_realationShip1);
        insert Upr_rel;
        //create Up Relationship attribute
        list<ICIX_V1__UP_Relationship_Attribute__c> attri=Hasbro_TestDataFactory.createUpRealtionShipAttribute();
        attri[0].ICIX_V1__UP_Relationship__c=Upr_rel[1].id;
        insert attri;
        System.Test.Starttest();
        UPRSync sh1 = new UPRSync (AssortProd[0].Id);
        database.executebatch( new UPRSync (AssortProd[0].Id));
        JobRunnerUPRSync Job=new JobRunnerUPRSync(10,'select ICIX_V1__Product1__c,ICIX_V1__Product2__c from ICIX_V1__PP_Relationship__c',1);
        String sch = '0 0 23 * * ?'; 
        system.schedule('Test Territory Check', sch, Job); 
        System.Test.StopTest();  
    }
    catch(Exception e){}
}
     
}
SwethaSwetha (Salesforce Developers) 
HI Santhiya,

I see that you have called the execute method in your test class like   
database.executebatch( new UPRSync (AssortProd[0].Id));
The code looks ok to me. I suspect the issue is that the start method is not getting any record from the query, and so your execute method is not getting executed.In order to fix it, ensure you have at least one record to process for the execute method to run.

It is a good practice to use System.assert() statements that verify that the data setup was correct before moving on to behavior assertions as stated in post (https://salesforce.stackexchange.com/questions/124256/test-not-cover-the-execute-method-in-batch).

Related: https://www.infallibletechie.com/2017/03/batch-class-execute-method-not-covered.html

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
santhiya duraisanthiya durai
Hi @Swetha
 this is start method I have written. There is no error but not covering execute method.

 @isTest Static void testUPRSync4(){
    string AssortmentId='NAS';
 //Create Responder Account
        Account acc=new Account();
        acc.name='Hasbro Dev Stg Res 2';
        acc.ICIX_V1__Status__c='Active';
        acc.ICIX_V1__ICIX_ID__c='304076';
        acc.ICIX_V1__Internal__c=true;
        acc.facility_name__c='Hasbro Dev Stg Res 2';
        insert acc;
        //create the product
        list<ICIX_V1__ICIX_Product__c> AssortProd=Hasbro_TestDataFactory.createProduct();
        //Create RetailItem Product
        list<ICIX_V1__ICIX_Product__c> RetailProd=Hasbro_TestDataFactory.createReatilItem();
        //create the PP Relation 
        list<ICIX_V1__PP_Relationship__c> PPr=Hasbro_TestDataFactory.createPP_relationship();
        PPr[0].ICIX_V1__Status__c='Active';
        PPr[0].ICIX_V1__Product1__c=AssortProd[0].id;
        PPr[0].ICIX_V1__Product2__c=RetailProd[0].id;
        PPr[0].ICIX_V1__type__c='Component';
        Update ppr;
         system.assertEquals(true, ppr.size()>0);
          Id AssortId;
         AssortmentId=AssortId;
         AssortmentID=PPr[0].ICIX_V1__Product1__c;
       List<ICIX_V1__PP_Relationship__c>pplist=new List<ICIX_V1__PP_Relationship__c>([select ICIX_V1__Product1__c,ICIX_V1__Product2__c from ICIX_V1__PP_Relationship__c where ICIX_V1__Product2__c !=null and ICIX_V1__Type__c='component' and ICIX_V1__Product1__c=:AssortmentID LIMIT 200]);
        
         system.assertEquals(true, pplist.size()>0);
         UPRSync sh1 = new UPRSync(ppr[0].id);
          Database.BatchableContext BC; 
         Database.QueryLocator ql = sh1.start(bc);
         //sh1.start(bc);
     }
SwethaSwetha (Salesforce Developers) 
Can you please confirm if the system.assertEquals() return the expected results?Thanks
santhiya duraisanthiya durai
@Swetha, System.assertEquals() return expected result i.e true.