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
santhanamsanthanam 

[Test Method] Need to insert 5000 records through test methods (More than 500 Records)

I have an Apex code which has If, else if & else clause based on the record count of a particular list variable, i.e

 

       

for( List<Offer__c> alloffers : [select Id,Shell_Operating_Entity__c,Account__c,Account__r.Name,Stage__c,Name from Offer__c where Account__r.Shell_Operating_Entity__c =:SOU and Account__r.Customer_Category__c='I&c' and Stage__c!='Not Used' and createddate>=:startdate and createddate<=:enddate])
// so upto 10,000 records can be fetched           

{
                       
            System.debug('alloffers size : '+alloffers.size());
            
            for(Integer j=0; j<alloffers.size(); j++)
                {
                System.debug('****alloffers[j].Stage__c****  '+alloffers[j].Stage__c );
                if(alloffers[j].Stage__c == 'Accepted')
                    {
                        System.debug('****offAccMap.size****: '+offAccMap.size());
                        if(offAccMap.size() < 1000)
                            {  
                   
                            if(!offAccMap.containsKey(alloffers[j].Account__c))
                                {

                                offAccMap.put(alloffers[j].Account__c,1);
                                offerAccepted+=1;
                                }
                            }

                         else if(offAccMap.size() < 2000)
                            {
                             if(!offAccMap.containsKey(alloffers[j].Account__c))
                                {

                                //FURTHER LINES OF CODE............

 

 

 

In this particular code, I am not able to write test method for the lines executed as a result of the if statement (if(offAccMap.size() < 1000))....

 I understand that, I can insert a maximum of 500 records in a test method, 

 

But, if these lines of code is not covered, My test method is not reaching 75%.

 

Is there any work around for this to write a test method so that the if,else if part of the code is covered ?

JonPJonP

You can't test it when all the code is in one gigantic method, but if you refactor it to move the query to another class, in your test you can substitute a mock object in place of your querying class that returns a list of 5000+ Offer_c objects by creating them in memory.

 

Take a look at this rough example I threw together:

 

 

1. Interface for the query class

 

public interface GetOpportunitiesIF { List<Opportunity> queryOpportunities(String country, Datetime startdate, Datetime enddate); }

 

 

2. Main class with business logic

 

public class PrimaryClass { public GetOpportunitiesIF getOpptsHelper = new GetOpportunitiesImpl(); public void doSomething(String stage, Datetime startdate, Datetime enddate) { List<Opportunity> allOpportunities = getOpptsHelper.queryOpportunities(stage, startdate, enddate); if (allOpportunities.size() > 5000) { // handle large data volume case } else { // handle small data volume case } } // end doSomething() public class GetOpportunitiesImpl implements GetOpportunitiesIF { public List<Opportunity> queryOpportunities(String stage, Datetime startdate, Datetime enddate) { return [select Id,Name,StageName from Opportunity where StageName = :stage and createddate>=:startdate and createddate<=:enddate]; } } }

 

3. Test class with separate unit tests for business logic and database query, and inner mock object class

 

@isTest private class TestPrimaryClass { static testMethod void testSmallDataVolume() { GetOpportunitiesMock goMock = new GetOpportunitiesMock(500); Test.startTest(); PrimaryClass pc = new PrimaryClass(); pc.getOpptsHelper = goMock; pc.doSomething('US', Datetime.now().addDays(-30), Datetime.now()); // Assert something } static testMethod void testLargeDataVolume() { GetOpportunitiesMock goMock = new GetOpportunitiesMock(10000); Test.startTest(); PrimaryClass pc = new PrimaryClass(); pc.getOpptsHelper = goMock; pc.doSomething('US', Datetime.now().addDays(-30), Datetime.now()); // Assert something } static testMethod void testGetOpportunitiesImpl() { GetOpportunitiesMock goMock = new GetOpportunitiesMock(200); // generate dummy opps List<Opportunity> testOpps = goMock.queryOpportunities('', Datetime.now(), Datetime.now()); insert testOpps; Test.startTest(); PrimaryClass pc = new PrimaryClass(); List<Opportunity> opps = pc.getOpptsHelper.queryOpportunities('US', Datetime.now().addDays(-1), Datetime.now()); System.assertEquals(200, opps.size()); } private class GetOpportunitiesMock implements GetOpportunitiesIF { private Integer numOpps; public GetOpportunitiesMock(Integer numOpps) { this.numOpps = numOpps; } public List<Opportunity> queryOpportunities(String country, Datetime startdate, Datetime enddate) { List<Opportunity> oppList = new List<Opportunity>(); for (Integer i=0; i<numOpps; i++) { oppList.add(new Opportunity( Name='TestOpp'+i /* more init values */ )); } return oppList; } } }

 

 

 

 

 

 

santhanamsanthanam

Thanks for the quick response. Let me try and see how it works.