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
Allen2Allen2 

please help me out to write the test class for this batchable class.....

APEX CLASS

global class S_BchICaseG implements Database.Batchable<sObject> , Schedulable {
    
    public static final String sysGenerated = 'System Generated';
    public static final String instalOrDeInstall = 'Install/De-Install';
    public static final String fieldService = 'Field Service';
    
     public static final List<String> IPre = new list<String> { 'S self install' , 'S self deinstall' };
 
    List<String> LstCo = System.Label.List_Of_Co.split(',');
    
    global void execute(SchedulableContext sc) {

        S_BchICaseG batch = new S_BchICaseG();
        system.debug('batch executed');
        Database.executeBatch(batch, 1);
    }

  
    global Database.Querylocator start(Database.BatchableContext BC){
       
        String query = 'Select ID, P_Case_Cr_Trig__c, S_Pt_Case_Cre_Trig__c, P_Mn_Itm__c, S_Case_Cate__c, S_Tk_Ref__c, S_Carrier__c, S_Pt_Case_No__c, S__Contact__c, P_DelNo__c From S__Inst_Pd__c WHERE P_Case_Cr_Trig__c  = true And S_Pt_Case_Cre_Trig__c = false And P_Mn_Itm__c = true And (S_Case_Cate__c != null Or S_Case_Cate__c != \'\') AND S_Case_Cate__c NOT IN : IPre ';
        Obj_Lkd__c CS = Obj_Lkd__c.getOrgDefaults();        
        if (CS != null && CS.Lk_JBch_Int__c && !LstCo.isEmpty())
        {
            query = query +' AND S__Site__r.S__Account__r.Country_I2__c NOT IN :LstCo ORDER BY lastmodifieddate asc ';
        }
        Else
        {
            query = query +' ORDER BY lastmodifieddate asc ';
        }

        system.debug('query picked up the records');
        return Database.getQueryLocator(query);    
    }
    
    global void execute(Database.BatchableContext BC,List <sObject> scope)
    {
        List<Case> newCaseList = new List<Case>();
        String tsId = Case_RecordType__c.getInstance(P_Constant.NameTech_Sup).Id__c;
        Boolean isException = false;
        system.debug('Execute method');
        
        if(System.Now().minute() > 56 && System.Now().minute() < 59 && !Test.isRunningTest())
          System.abortJ(bc.getJId());
        
        for (S__Inst_Pd__c ip : (List<S__Inst_Pd__c>) scope)
        {
        system.debug('Entered for loop for case creation');
            Case caseRec = new Case();
            caseRec.S_Cs_Carrier__c = ip.S_Carrier__c;
            caseRec.Categories__c = fieldService;
            caseRec.S__Compo__c = ip.Id;
            caseRec.ContactId = ip.S__Contact__c;
            caseRec.Inst_Del_No__c = ip.P_DelNo__c;
            caseRec.PrntId = ip.S_Pt_Case_No__c;
            caseRec.Pu_Ord__c =  instalOrDeInstall ;
            caseRec.Reasons__c = sysGenerated; 
            caseRec.RecordTypeId = tsId ;
            caseRec.Su_Rsn__c = ip.S_Case_Cate__c;
            caseRec.S_Cs_Tk_Ref__c = ip.S_Tk_Ref__c;            
            caseRec.Subject = Label.P_Cs_Sub_&_Desc_Fr_Gb;
            caseRec.Description = Label.P_Cs_Sub_&_Desc_Fr_Gb;
            newCaseList.add(caseRec);
            
            ip.P_Case_Cr_Trig__c = false;
            ip.S_Carrier__c = '';
            ip.S_Tk_Ref__c = '';
        }
        
        SMWUtils.inBatchMode = true;
        System.debug('Begining of case creation');
        try{
            insert newCaseList;
        }catch (Exception e){
            system.debug('Exception Caught :'+e);
            system.debug('Exception in batch class S_BchICaseG - '+e.getMessage());
            isException=true;
        }
        System.debug('End of case creation :'+scope +isException);
        if(!isException)
            update scope; 
            System.debug('Exit of Case creation');       
    }

    
    global void finish(Database.BatchableContext BC)
    {
            
    }   
}

TEST CLASS


@isTest
private class S_BchICaseG_test {

    static testMethod void myUnitTest() {
        
        Product2 prod = new Product2(Name = 'Test');
        insert prod;
        
        S__Site__c site = new S__Site__c(
            Name = 'Test',
            S__Country__c = 'United Kingdom'
        );
        
        insert site;
        List<Case_RecordType__c> CaseRecordTypedata=P_TstDataFactory.crCustomSettingdataCase();
        if(!CaseRecordTypedata.isEmpty())
            insert CaseRecordTypedata;
       
        List<S__Inst_Pd__c> ipList = new List<S__Inst_Pd__c>();
        
        S__Inst_Pd__c ip = new S__Inst_Pd__c(
            Name = 'test', 
            P_CS_CrTRIG__c = true, 
            P_CS_CrTRAN_CD__c = '01',
            S__Site__c = site.Id,
            P_Sls_Org__c = 'DMT'
        );
        ip.P_Case_Cr_Trig__c = true;
        ip.S_Pt_Case_Cre_Trig__c = false;
        ip.P_Mn_Itm__c = true;
        ip.S_Case_Cate__c = 'S install';
        
        ipList.add(ip);
        
        insert ipList;

        Test.startTest();     
        
        try{
            S_BchICaseG batch = new S_BchICaseG();
            Database.executeBatch(batch);
        }catch(Exception e){
            System.debug('##### Exception ######'+e.getMessage());
        }
  
        Test.stopTest();       
    }
}

the bold italic and underlined part is not able to cover please help me out to cover this code.....
Raj VakatiRaj Vakati
The issue is your query is not returning any data to execute mehod set the S__Site__r.S__Account__r value to the S__Inst_Pd__c object  to meet your  where condition 

// Add the  S__Site__r.S__Account__r.Country_I2__c  here  
 
List<S__Inst_Pd__c> ipList = new List<S__Inst_Pd__c>();
        For( Integer i = 0 ;i <10 ;i++){
        S__Inst_Pd__c ip = new S__Inst_Pd__c(
            Name = 'test', 
            P_CS_CrTRIG__c = true, 
            P_CS_CrTRAN_CD__c = '01',
            S__Site__c = site.Id,
            P_Sls_Org__c = 'DMT'
        );
		// Add the  S__Site__r.S__Account__r.Country_I2__c  here 
        ip.P_Case_Cr_Trig__c = true;
        ip.S_Pt_Case_Cre_Trig__c = false;
        ip.P_Mn_Itm__c = true;
        ip.S_Case_Cate__c = 'S install';
        
        ipList.add(ip);
        }
        insert ipList;
@isTest
private class S_BchICaseG_test {

    static testMethod void myUnitTest() {
        
        Product2 prod = new Product2(Name = 'Test');
        insert prod;
        
        S__Site__c site = new S__Site__c(
            Name = 'Test',
            S__Country__c = 'United Kingdom'
        );
        
        insert site;
        List<Case_RecordType__c> CaseRecordTypedata=P_TstDataFactory.crCustomSettingdataCase();
        if(!CaseRecordTypedata.isEmpty())
            insert CaseRecordTypedata;
       
	   
        List<S__Inst_Pd__c> ipList = new List<S__Inst_Pd__c>();
        For( Integer i = 0 ;i <10 ;i++){
        S__Inst_Pd__c ip = new S__Inst_Pd__c(
            Name = 'test', 
            P_CS_CrTRIG__c = true, 
            P_CS_CrTRAN_CD__c = '01',
            S__Site__c = site.Id,
            P_Sls_Org__c = 'DMT'
        );
		// Add the  S__Site__r.S__Account__r.Country_I2__c  here 
        ip.P_Case_Cr_Trig__c = true;
        ip.S_Pt_Case_Cre_Trig__c = false;
        ip.P_Mn_Itm__c = true;
        ip.S_Case_Cate__c = 'S install';
        
        ipList.add(ip);
        }
        insert ipList;

        Test.startTest();     
        
        try{
            S_BchICaseG batch = new S_BchICaseG();
            Database.executeBatch(batch);
        }catch(Exception e){
            System.debug('##### Exception ######'+e.getMessage());
        }
  
        Test.stopTest();       
    }
}


 
Allen2Allen2
Hi Raj,
I am not able to pass the vale of object to this the  S__Site__r.S__Account__r.Country_I2__c  ... please tell me how to pass this...
 
Raj VakatiRaj Vakati
S__Site__r.S__Account__r is an look up to account ??