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
Isha Saxena 18Isha Saxena 18 

How to write the test class for this test class

I am really trying to cover the forllowing batch with test class please help :

Batch class:

/*
Name: BatchOpportunitySnapshotReportCreation
Copyright © 2019 SAI Global

Purpose:CMPS-2557 On reporting on opportunity records by performing aggregation using multiple criteria
   
VERSION  AUTHOR            DATE             DETAIL                FEATURES/CSR/TTP
    1.0  Isha Saxena     17 July 2019         Inital Creation
*/

global class BatchOpportunitySnapshotReportCreation implements Database.Batchable<SObject>,Database.Stateful
{
     global final Set<Id> ownerList = new Set<Id>();
     global string exceptn= '';
     global string query; 
     global boolean isTestRunning;
      List<User> FYoppOwners = new List<User>();
     global BatchOpportunitySnapshotReportCreation()
     {
         System.debug('Constructor calling@@@@@@');
         FYoppOwners = [SELECT Id,Name,Team__c,Area__c,CurrencyIsoCode FROM User WHERE Id IN (SELECT OwnerId FROM Opportunity WHERE CloseDate = THIS_FISCAL_YEAR)];
         System.debug('Constructor calling@@@@@@'+FYoppOwners);
         if(FYoppOwners.size()>0)
         {
             for(user uid : FYoppOwners)
             {
                 ownerList.add(uid.Id);
             }
         }
     }   
     global database.QueryLocator start(Database.BatchableContext BC)
    {
        System.debug('Opportunity Owner list&&&&&&'+ownerList);
       
        List<Opportunity> oppReport = new List<Opportunity>();
         if(isTestRunning==null)
         {
            isTestRunning = false;
         }  
         
         query = 'SELECT Id,Total_First_Year_Revenue__c ,X1st_Year_Expected_Revenue__c,Probability,ExpectedRevenue,Amount,OwnerId,Owner.CurrencyIsoCode,Days_took_to_Close__c,Owner.Name,Owner.Team__c,Owner.Area__c,StageName' +
                        ' FROM Opportunity' +
                          ' WHERE OwnerId IN : ownerList' +
                          ' AND CloseDate = THIS_FISCAL_YEAR'+
                        ' ORDER BY Owner.Name,Owner.Team__c,Owner.Area__c,StageName';
         System.debug(LoggingLevel.DEBUG, query);
         return Database.getQueryLocator(query);
        
    }
    global void execute(Database.BatchableContext BC, List<Opportunity> opptysToReport)
    {
        try
        {
            
            System.debug('execute function@@@@@@@@'+opptysToReport);
            if(opptysToReport.size()>0)
            {
                    OpportunitySnapshotHelper.executeSnapshotCreation(opptysToReport,FYoppOwners);
            }
        }
        catch(Exception e)
        {
             exceptn += e.getMessage();  
            System.debug('@@@@@@Exception'+exceptn);
        }
        
    }
    global void finish(Database.BatchableContext BC)
    {
       
        
         System.debug(LoggingLevel.WARN,'Opportunity Snapshot is Complete!');
     }
}


Please help me with the test class
   



 
Durga PaavanDurga Paavan
Can you please provide test class you wrote for this class.

Cheers,
Durgapaavan
Isha Saxena 18Isha Saxena 18
Hi Durga,

Thanks a lot in advance.

/**
* @company      SAI Global
* @author       Isha Saxena (jason.guan@saiglobal.com)
* @date         2/08/2019
* @description  Test class for BatchOpportunitySnapshotReportCreation batch   
*/

@isTest
public class TestBatchOpportunitySnapshotReport 
{
    @isTest
    private static void testdata()
    {
        List<Opportunity> oppList = new List<Opportunity>(); 
           Account client = TestDataCreator.createClient();
        insert client;
        Account clientSite1 = TestDataCreator.createClientSite(client);
        insert clientSite1;
        Opportunity opp = TestDataCreator.createOpportunity(client);
        insert opp;
        oppList.add(opp);
         User u = new User();
         u.FirstName = 'abc';
         u.LastName = 'test123';
         u.email = 'abc@test123.com';
         insert u;
         for(Integer i=0 ;i <200;i++)
         {
            Opportunity opp1 = TestDataCreator.createOpportunity(client);
            opp1.Name ='Name'+i;
            opp1.OwnerId = u.Id;
            opp1.StageName = 'Closed Won';
            opp1.Won_Reason__c = 'A reason to Win';
            opp1.CloseDate = System.today();
            opp1.Amount = 1000;   
            oppList.add(opp1);
         }
         insert oppList;

        
        
       
        Opportunity_Certification__c optyCert = TestDataCreator.createOpptyCert(opp);
        insert optyCert;
        
        Opportunity_Site_Certification__c optySiteCert = TestDataCreator.createOpptySiteCert(opp, clientSite1);
        insert optySiteCert;   
        
        Program__c prog = TestDataCreator.createProgram();
        insert prog;
        
        Standard__c standard1 = TestDataCreator.createStandard(prog.ID);
        insert standard1;
        
        Standard__c standard2 = TestDataCreator.createStandard(prog.ID);
        insert standard2;
        
        Standard_Program__c sProg1 = TestDataCreator.createStandardProgram(standard1, prog);
        insert sProg1;
        
        Standard_Program__c sProg2 = TestDataCreator.createStandardProgram(standard2, prog);
        insert sProg2;
        
        Oppty_Cert_Standard_Program__c optyCertStndProg = TestDataCreator.createOpptyCertStdProg(optyCert,sProg1);
        insert optyCertStndProg;
        
        Oppty_Site_Cert_Standard_Program__c optySiteCertStndProg = TestDataCreator.createOpptySiteCertStdProg(optySiteCert, sProg1);
        insert optySiteCertStndProg;
        
        Code__c code = TestDataCreator.createCode();
        insert code;
        
        Standard_Code__c stdCode = TestDataCreator.createStandardCode(standard1);
        insert stdCode;
       

        
      }
    @isTest    
    private static void testOppSnaphotmethod()
    {
                Test.startTest();
                     
           BatchOpportunitySnapshotReportCreation batchJob = new BatchOpportunitySnapshotReportCreation(); 
          ID batchJobID = Database.executeBatch(batchJob,10);
        
       Test.stopTest();
     }
}

It's just covering 59 percent