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
sieb4mesieb4me 

can someone please help me with test class for this batch class...its urgent..thanks.

Hi,

I have completed the batch class and scheduler class which works fine but need help in creating test class.

Can someone please guide me on how to begin with it.

 

Batch class:

global class oppupdates implements Database.Batchable<sObject>{
    public String query;
    public String email;
    public Set<Id> setCaseId = new Set<Id>();
    //list <Case> objcase;
    public List<Opportunity> lstOppty= new List<Opportunity>();
    public List<Case> lstCase;
    public String SumLicense;
    public Integer CountOpportunity;
    public Double SLic = 0;
    public Decimal conRate=2;
    public String accId;
    
     
    global Database.querylocator start(Database.BatchableContext BC){
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope){
         lstCase = new list<Case>();// ([select id, Status from Case]);
                         
        for(sObject s : scope){Case c = (Case)s;
            {
                setCaseId.add(c.Id);
                      }
        }
          
        
        //objcase = trigger.new;
 List<Case> lstCase = [Select Id, AccountId,Status from Case where Id IN :setCaseId and AccountId != null 
order by AccountId];
List<Id> accountIdList = new List<Id>();
Map<Id, Case> account2CaseMap = new Map<Id, Case>();
List<Case> cs = new List<Case>();

 

// collect the account id's into a list and create a map of account id to case
for (Case case1 : lstCase) {
    accountIdList.add(case1.AccountId);
    account2CaseMap.put(case1.AccountId, case1);
}

 

// get opportunities for all accounts of interest
lstOppty = [Select Id, Name, Probability,accountid, Amount from Opportunity
where AccountId in : accountIdList
AND StageName IN ('Qualification','Prospecting')
AND CloseDate = THIS_QUARTER
AND CloseDate <= NEXT_QUARTER order by AccountId];


//CountOpportunity = lstOppty.size();
Id previousAcctId = null;
//SLic = 0;
//Decimal conRate = ?
Integer oppCount = 0;
Integer caseIndex = 0;
for (Opportunity opp: lstOppty){
    String thisAcctId = opp.AccountId;
    
    //system.debug('IIIIIIIIIIaccid' + thisAcctId);
   // system.debug('IIIIIIIIIIopp.accid' + opp.AccountId);
    
    if (thisAcctId != previousAcctId) {
        if (previousAcctId != null) {
            // skip past any non matching account id's - should not be any
            while ((caseIndex < lstCase.size()) && (lstCase[caseIndex].AccountId != previousAcctId)) 
                caseIndex++;
            while ((caseIndex < lstCase.size()) && (lstCase[caseIndex].AccountId == previousAcctId)) {
                Case c = lstCase[caseIndex];
            
                if ((c != null) && (c.Status !=null)) {
              
                    c.SumLicence__c = SLic;
                    
                    c.Total_opp__c = oppCount;
                   
                //c.Status = 'Working';
                    cs.add(c);            
                }
                caseIndex++;
            }
        }
        previousAcctId = thisAcctId;
        SLic = 0;
        oppCount = 0;
        
    }
    oppCount++;
  
                
                
    if(opp.Amount != null){
        SLic += opp.Amount*conRate;
      
    }

}
// handle the opportunities for the last account
            while ((caseIndex < lstCase.size()) && (lstCase[caseIndex].AccountId == previousAcctId)) {
                Case c = lstCase[caseIndex];
                if ((c != null) && (c.Status !=null)) {
                
                    c.SumLicence__c = SLic;
                    c.Total_opp__c = oppCount;
                //c.Status = 'Working';
                    cs.add(c);            
                }
                caseIndex++;
            }
           // Database.DMLOptions dml = new Database.DMLOptions();
          // dml.optAllOrNone = false; // tried true also                
          //  database.update(cs,false);
Update cs;
}      

        //SumLicense = SLic;
  
        
   
    global void finish(Database.BatchableContext BC){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setToAddresses(new String[] {email});
        mail.setReplyTo('batch@acme.com');
        mail.setSenderDisplayName('Batch Processing');
        mail.setSubject('Batch Process Completed');
        mail.setPlainTextBody('Batch Process has completed');
        
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

 

Scheduler class:

 

global class oppaccount implements Schedulable{ 
  global void execute(SchedulableContext SC) {
oppupdates opp = new oppupdates();
opp.query = 'Select Id, AccountId,Status from Case where Status =\'' + 'Working' + '\'';
opp.email='j1j2test@yahoo.com';
Database.executeBatch(opp);
}
}

 

s_k_as_k_a
ForcepowerForcepower

Sieb4me,

 

As s-k-a pointed out, there is an example at the end of that link. I think you will need to create some Test Accounts, Cases and Opportunities to match the criteria needed by your batch class as part of the test method. Then you can create the class, set it up and invoke it.

 

Here's the example:

 

List <Account> accns = new List<Account>();

for(integer i = 0; i<200; i++){

Account a = new Account(Name='testAccount'+'i', Ownerid = u.ID);

accns.add(a);

}

insert accns;

Test.StartTest();

OwnerReassignment reassign = new OwnerReassignment();

reassign.query='SELECT ID, Name, Ownerid ' + 'FROM Account ' + 'WHERE OwnerId=\'' + u.Id + '\'' + ' LIMIT 200'; reassign.email='admin@acme.com';

reassign.fromUserId = u.Id;

reassign.toUserId = u2.Id;

ID batchprocessid = Database.executeBatch(reassign);

Test.StopTest();

 

Ram

sieb4mesieb4me
Thank you Ram
sieb4mesieb4me
Thanks ska