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?
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.System.assertEquals(expectedValue, actualValue);
. It looks like you may have the expected and actual values the other way round.