2

I've done some searching and having trouble finding an answer to what I'm assuming is a bit of a noobish question.

I'm writing a test class for code which updates the owner of a lead. I'd like to write a unit test for a bulk insert scenario and assert the owner for the list of inserted leads was set properly. I don't want to use a for loop as it doesn't sound bulkified, but maybe that's the way to go.

Here is what I have for the test:

    //build test company leads for insert
    list<Lead> companyLeads = new list<Lead>();
    for (Integer i = 1; i <= 250; i++){
        Lead a = new Lead(FirstName='Test', LastName='Leada '+i, Title='CEO', Company='Consulting Co '+i,
                          Status='New', Phone='(555)-555-5555', Email='[email protected]',RecordTypeId = Util.getRecordTypeId('Lead', 'Client'),
                          Account__c=companyAccounts[0].id);
        companyLeads.add(a);
            }
    //Single record insert
    Lead l = companyLeads.remove(249);
    try{
        insert l;
    } catch (exception ex) {
        system.debug('Single Record Insert on LeadHelper.companyLeadAccountChange exception' + ex);
    }
    l = [SELECT Id, ownerId 
         FROM Lead 
         WHERE Id = :l.Id];
    system.assertEquals(l.ownerId, companyAccounts[0].ownerId);
    //Bulk record insert
    Database.SaveResult[] lsr = Database.Insert (companyLeads,false);

I would like to assert that every inserted record has their ownerId = companyAccounts[0].ownerId, just like the single record insert above it. Is it common practice to assert bulk insert or should I only concern myself with asserting single inserts?

2
  • 1
    If you know the expected OwnerId then you can include that in the SOQL. e.g. [select OwnerId from Lead where Id in :leadIds and OwnerId = :expectedOwnerId]. You then need to make sure that the correct number of records are returned.
    – BarCotter
    Commented Nov 11, 2014 at 18:01
  • 2
    On a side note, asserts by convention are in the format: System.assertEquals(expectedValue, actualValue);. It looks like you may have the expected and actual values the other way round.
    – BarCotter
    Commented Nov 11, 2014 at 18:03

2 Answers 2

2

You can use the SOQL statement to make sure that all the Leads have the expected OwnerId. If the OwnerId isn't set correctly then the SOQL will return less than 250 records in the below code.

//build test company leads for insert
List<Lead> companyLeads = new list<Lead>();
for (Integer i = 1; i <= 250; i++){
    Lead a = new Lead(FirstName='Test', LastName='Leada '+i, Title='CEO', Company='Consulting Co '+i,
                      Status='New', Phone='(555)-555-5555', Email='[email protected]',RecordTypeId = Util.getRecordTypeId('Lead', 'Client'),
                      Account__c=companyAccounts[0].id);
    companyLeads.add(a);
}
insert companyLeads;
List<Lead>  leads = [SELECT Id, ownerId 
     FROM Lead 
     WHERE Id in :companyLeads
     and OwnerId = :companyAccounts[0].ownerId];
System.assertEquals(companyLeads.size(), leads.size());
2

I house my common assertion methods in a public, @isTest-annotated utility Apex class called something like TestUtils. The most common bulk assertion methods I use are to verify that a single record or set of records were successfully inserted (in which case they will have been given Ids), so here are some utility methods that do that:

@isTest
public class TestUtils {

   public static void AssertInserted(List<SObject> objs) {
       for (SObject obj : objs) System.assertNotEquals(null,obj.Id);
   }

   public static void AssertInserted(SObject obj) {
        System.assertNotEquals(null,obj.Id);
   }

}

which could be used like this in your example:

//Bulk record insert
Database.SaveResult[] lsr = Database.Insert (companyLeads,false);
// Make sure our records were ALL inserted successfully
TestUtils.AssertInserted(companyLeads);

You could also extend field validation to be set generically in a similar manner, e.g.

public static void AssertFieldEquals(List<SObject> objs, String fieldName, Object expectedValue) {
      for (SObject obj : objs) System.assertEquals(expectedValue,obj.get(fieldName));
}

which could be used in your scenario like this:

TestUtils.AssertFieldEquals(companyLeads,'OwnerId', companyAccounts[0].ownerId);

You must log in to answer this question.

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