+ Start a Discussion
guptach45guptach45 

I have to write a test class for batch class. PLease help as i am new to apex coding.

global class CSTeamCountryCaseUpdateBatch implements Database.Batchable<sObject>{
   global Database.QueryLocator start(Database.BatchableContext bc) {
      return Database.getQueryLocator([SELECT ID, Status, LastModifiedDate,LastModifiedById,Last_Modified_By_Previous_Day__c,CS_Team_Country__c,Days_Since_Last_Modified_Date__c FROM Case Where Status !='Closed' AND (CS_Team_Country__c ='United States' OR CS_Team_Country__c ='Canada')]);
    }
    global void execute(Database.BatchableContext bc, List<Case> records){
        system.debug('records--'+records.size());
       List<Case> caseList = new List<Case>();
        for(Case c:records){
            system.debug('test--'+c.Days_Since_Last_Modified_Date__c);
            if(c.Days_Since_Last_Modified_Date__c == null){
                c.Days_Since_Last_Modified_Date__c =0;
            }
            c.Days_Since_Last_Modified_Date__c =c.Days_Since_Last_Modified_Date__c+1;
            if(c.LastModifiedById != system.label.apiuserid){
            c.Last_Modified_By_Previous_Day__c =c.LastModifiedById;
            }
            caseList.add(c);
        }
        system.debug('caseList--'+caseList);
        if(caseList.size()>0){
            update caseList;
        }
        system.debug('caseList--'+caseList);
    }    
    global void finish(Database.BatchableContext bc){
        
    }   
}
Best Answer chosen by guptach45
shaik murthujavalishaik murthujavali
Hi Sakshi,
Try this below code.
@isTest
private class CaseBatchTest{
    @testSetup 
    static void setup() {
        list<Case> listtoInsert = new list<Case>();
        Case cs = new Case();
        cs.Status = 'Closed';
        cs.CS_Team_Country__c ='United States';
        listtoInsert.add(cs);
        Case cs = new Case();
        cs.Status = 'Closed';
        cs.CS_Team_Country__c ='Canada';
        listtoInsert.add(cs);
        insert listtoInsert;
    }
    static testmethod void testthedata() {
        Test.startTest();
        CSTeamCountryCaseUpdateBatch csbatch = new CSTeamCountryCaseUpdateBatch();
        Id jobId = Database.executeBatch(csbatch);
        Test.startTest();
        
    }
}

If it works for you pls mark it as a best answer so it helps others too.
Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Sakshi,

I used trailhead example I hope you find this useful for your use case.
 
@isTest
private class CSTeamCountryCaseUpdateBatchTest{
    @testSetup 
    static void setup() {
        Insert the records that could satisfy the soql filters so that they can be queried.
    }
    static testmethod void test() {        
        Test.startTest();
       Perform the business logic you would like to perform so that the lines are covered
        Test.stopTest();
        // after the testing stops, assert records were updated properly
        System.assertEquals(10, [select count() from contact where MailingCity = 'New York']);
    }
    
}

The trailhead link is: https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_batch.

Do let me know if there are any issues or in case if this worked can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej
shaik murthujavalishaik murthujavali
Hi Sakshi,
Try this below code.
@isTest
private class CaseBatchTest{
    @testSetup 
    static void setup() {
        list<Case> listtoInsert = new list<Case>();
        Case cs = new Case();
        cs.Status = 'Closed';
        cs.CS_Team_Country__c ='United States';
        listtoInsert.add(cs);
        Case cs = new Case();
        cs.Status = 'Closed';
        cs.CS_Team_Country__c ='Canada';
        listtoInsert.add(cs);
        insert listtoInsert;
    }
    static testmethod void testthedata() {
        Test.startTest();
        CSTeamCountryCaseUpdateBatch csbatch = new CSTeamCountryCaseUpdateBatch();
        Id jobId = Database.executeBatch(csbatch);
        Test.startTest();
        
    }
}

If it works for you pls mark it as a best answer so it helps others too.
Thanks.
This was selected as the best answer
guptach45guptach45
Thanks a lot