1

I am creating a script that gets run every quarter that checks for any paid status in campaignMember objects that has contactId attached, and update the status to unpaid and create a campaignMember object with each contactId for the next quarter. My unit tests run successfully with 200 records, but I can't get it to run with more than that. I would like to make sure that I am testing with at least 1500 records in the end.

This test setup method gets CPU limit error when I set the numberOfUserToCreate to more than 200 or so.

@testSetup static void setupCotacts() {

List<Contact> con = new List<Contact>();
for(Integer i = 0; i < numberOfUserToCreate; i++) {
        con.add(new Contact(
            FirstName= 'abc'+i,
            LastName= 'Smith' + i,
            MailingCity= 'Seattle',
            MailingCountryCode= 'US',
            MailingPostalCode= '23688',
            MailingStreet= '123 street '+ i + 'th ave',
            MiddleName= 'j',
            Personal_Email__c= 'abc' + i +'@gmail.com',
            Preferred_First_Name__c= 'abc'+ i,
            Preferred_Last_Name__c= 'Smith' + i,
            Preferred_Middle_Name__c= 'j',   
        ));
    }
    insert con;
    System.debug('Contacts : ' + con.size());

  }
4
  • 1
    It is impossible to do more than just guess without knowing the state of your org. You could have long running Process Builder flows, inefficient Apex Triggers, etc.
    – Adrian Larson
    May 16, 2019 at 17:22
  • I guess I would ask why you need to do this; when operating against 1500 objects one is inclined to use a batch class and you can simply test against the batch execute's scope size
    – cropredy
    May 16, 2019 at 17:58
  • @cropredy if my script needs to process 1500 contact records, shouldn't I have 1500 records created before testing my script even if I used batch to query 200 at a time in my script? I'm new to apex, so any guidance would be appreciated.
    – il0v3d0g
    May 16, 2019 at 18:36
  • if your script is a batch apex, then for 1500 recs, there will be 2 + n transactions - (1) start() that queries - no real reason to worry about limits unless you have more than 50million; (2) execute() one Txn per 200 recs; (3) finish() - unusual to have limits issues here. So, you merely need to test that an execute() can successfully process 200 records if you are worried about exceeding limits
    – cropredy
    May 16, 2019 at 18:41

2 Answers 2

2

Few days ago, I came to know that we can use Test.startTest() and Test.stopTest() in methods annotated with @TestSetup. What this means is that we can do 3x governor limits in a test method.

Example: By default only 10000 DML records are supported per transaction but in the below example, I am able to insert total of 30000 DML records.

@isTest
public class ClassA_TEST {

    @TestSetup
    static void makeData(){
        Test.startTest();
        insertTasks(10000);
        Test.stopTest();
    }

    @isTest
    private static void scenarioA(){
        insertTasks(10000);

        Test.startTest();
        insertTasks(10000);
        Test.stopTest();

        Integer noOfRecords = [SELECT Id FROM Task].size();

        System.debug(LoggingLevel.ERROR, noOfRecords);
    }

    private static void insertTasks(Integer noOfRecords){
        List<Task> taskList = new List<Task>();
        for (Integer i =0 ; i< noOfRecords; i++) {
            taskList.add(new Task());
        }
        insert taskList;
    }

}
1
  • I was able to successfully create 1500 records for contact objects! Appreciate your precise instruction!
    – il0v3d0g
    May 16, 2019 at 17:54
1

Why not create a CSV , upload it in static resource and load it in test content using test.loadData as

List<sObject> ls = Test.loadData(Contact.sObjectType, 'myResource');

Src: Loading Test Data

2
  • Thank you for your prompt response! I will try that, and let you know how it goes.
    – il0v3d0g
    May 16, 2019 at 15:42
  • I created a csv file with 1500 contact records, loaded data from SF ui, and called Test.loadData, and I got 'System.SObjectException: common.apex.runtime.impl.ExecutionException: Apex CPU time limit exceeded' error on the line. Plz assist. Thank you!
    – il0v3d0g
    May 16, 2019 at 17:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .