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 

Hi everyone... help me to write test class for the below batch class...

BATCH CLASS

global class Chatterpostsbatch implements Database.batchable<sObject> {

global String queryOpps;

global Chatterpostsbatch(String query){
  queryOpps=query;
  system.debug(queryOpps);
}

global Database.QueryLocator start(Database.BatchableContext bc)
  {
    system.debug(queryOpps);
      return Database.getQueryLocator(queryOpps);
    }
    
    global void execute(Database.BatchableContext bc, List<SObject> lstOpps)
  {
    set<Id> setOppIds = new set<Id>();
    List<EntitySubscription> followers = new List<EntitySubscription> ();
    
    for(SObject o : lstopps)
    {
      setOppIds.add(o.Id);
    }
    if(!setOppIds.Isempty()){   
      System.debug(setOppIds);
        followers =  [select id from EntitySubscription
            where parentid in: setOppIds];
    }
        if(!followers.Isempty()){                    
        delete followers;
        }                  
  }
  
  global void finish(Database.BatchableContext bc)

  {
    //Mail notification...
    //empty;
  }
}

TEST CLASS

@isTest

private class TestClassChatterPosts {
    
    static testMethod void testChatterPostsScheduled() 
    {
        Test.StartTest();
        Chatterpostsschedule cp = new Chatterpostsschedule();
        datetime currentDateTime = datetime.now();
        String min = String.valueOf(math.mod((currentDateTime.minute() + 1),60));
        String schExp = '0 '+ min +' * * * ? ';
        system.schedule('ChatterPost - ' + system.now().format(), schExp, cp);     
        Test.StopTest();
    }
    
    static testMethod void testChatterPostsBatch() 
    {
        //String queryOpps = 'select Id from Opportunity where IsClosed=true and CloseDate <= LAST_N_DAYS:30 limit 1';
        CustomSettingCreation.SettingData();
        
        Account acc = new Account(Name = 'Test');
        insert acc;
        
        Opportunity opp = new Opportunity();        
        opp.name = 'Test';
        opp.AccountId = acc.id;
        insert opp;
        
        EntitySubscription ens = new EntitySubscription();
        insert ens;
        
        Test.StartTest();
        
        String opps = opp.name;
        Chatterpostsbatch   obj = new  Chatterpostsbatch('Test');
        Database.executeBatch(obj);
        
        Test.StopTest();
    }
}

The second method is failing at the place created the instance of the batch. The bold and underlined part unable to cover. Please someone help me to correct it....
Maharajan CMaharajan C
Hi Amy,

Please find the below updated test class. It will give the 100% coverage:


@isTest

private class TestClassChatterPosts {

  static testMethod void testChatterPostsScheduled() 
    {
        Test.StartTest();
        Chatterpostsschedule cp = new Chatterpostsschedule();
        datetime currentDateTime = datetime.now();
        String min = String.valueOf(math.mod((currentDateTime.minute() + 1),60));
        String schExp = '0 '+ min +' * * * ? ';
        system.schedule('ChatterPost - ' + system.now().format(), schExp, cp);     
        Test.StopTest();
    }

static testMethod void testChatterPostsBatch()
{

Profile p = [select id from profile where name = 'System Administrator'];

User userrec = new User(alias = 'test01', email='test01@testorg.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='America/Los_Angeles', username='test01@testorg.com');
insert userrec;


Account acc = new Account(Name = 'Test');
insert acc;

Opportunity opp = new Opportunity();
opp.name = 'Test';
opp.AccountId = acc.id;
opp.StageName = 'Prospect';               // Change the Stagename as per your Oppotunity Stages
opp.CloseDate = system.today();

insert opp;

EntitySubscription ens = new EntitySubscription(parentid = opp.Id,SubscriberId=userrec.Id);
insert ens;

Test.StartTest();

String opps = opp.name;
Chatterpostsbatch obj = new Chatterpostsbatch('Select Id from Opportunity');
Database.executeBatch(obj);

Test.StopTest();
}
}


Thanks,
Maharajan.C