I have a Test Class which retrieves a list of junction object records, created earlier in the Class & deletes them.
List<SellerxProperty__c> sxpJosToDel = [SELECT Id, Property__c FROM SellerxProperty__c
WHERE Property__c IN :propertiesForTest];
delete sxpJosToDel;
system.debug('sxpJosToDel contains ' + sxpJosToDel);
system.assertEquals(0,sxpJosToDel.size());
But when I use system.assert
to check that the list sxpJosToDel
is empty, at the end of my Test Class, the test fails because the size of the list is still 200 (& the records in that list are picked up by my debug statement).
Since I've deleted the list on the line before my debug statement & assert, I don't understand why the list isn't empty?
To give the full background..I've created a Trigger which deletes junction object (SellerContactxProperty__c
) records linking Contacts
to Property__c
records, when junction object (SellerxProperty__c
) records that link Property__c
records to the Accounts
(which the Contacts
are related to), are deleted.
trigger DeletedSellerPropertyJo on SellerxProperty__c (before delete) {
Set<Id> sellerAccounts = new Set<Id>();
Set<Id> properties = new Set<Id>();
List<SellerContactxProperty__c> junObjsToDel = new List<SellerContactxProperty__c>();
for(SellerxProperty__c jo : Trigger.Old){
sellerAccounts.add(jo.Seller_Account__c);
properties.add(jo.Property__c);
}
List<Contact> accountContacts = [SELECT Name, Id, AccountId,
(SELECT Id FROM SellerContactxProperty__r)
FROM Contact
WHERE AccountId IN :sellerAccounts];
for(Contact c : accountContacts){
SellerContactxProperty__c scxp = c.SellerContactxProperty__r;
junObjsToDel.add(scxp);
}
delete junObjsToDel;
}
The SellerContactxProperty__c
records are created by a Trigger when SellerxProperty__c
records are created.
So in my Test Class I just need to create the Account, Contact & Property records, add the SellerxProperty__c
to cause the Trigger to create the SellerContactxProperty__c
records, then retrieve the newly created SellerxProperty__c
records & delete them so that my Trigger also deletes the SellerContactxProperty__c
records.
@isTest
private class DeletedSellerPropertyTestJo {
static testMethod void testJoDelete(){
List<Account> acctsForTest = new List<Account>();
List<Properties__c> propertiesForTest = new List<Properties__c>();
List<Contact> contactsForTest = new List<Contact>();
List<SellerxProperty__c> sxpJosForTest = new List<SellerxProperty__c>();
for (Integer i = 0; i < 200; i++){
Account a = new Account();
a.Name = 'Test Account' + i;
acctsForTest.add(a);
}
insert acctsForTest;
for (Integer i = 0; i < 200; i++){
Properties__c p = new Properties__c();
p.Name = 'Test Property ' + i;
propertiesForTest.add(p);
}
insert propertiesForTest;
for (Integer i = 0; i < 200; i++){
Contact c = new Contact();
c.LastName = 'Test Contact ' + i;
c.AccountId = acctsForTest[i].Id;
contactsForTest.add(c);
}
insert contactsForTest;
for (Integer i = 0; i < 200; i++){
SellerxProperty__c sxp = new SellerxProperty__c();
sxp.Seller_Account__c = acctsForTest[i].Id;
sxp.Property__c = propertiesForTest[i].Id;
sxpJosForTest.add(sxp);
}
insert sxpJosForTest;
List<SellerxProperty__c> sxpJosToDel = [SELECT Id, Property__c FROM SellerxProperty__c
WHERE Property__c IN :propertiesForTest];
delete sxpJosToDel;
system.debug('sxpJosToDel contains ' + sxpJosToDel);
system.assertEquals(0,sxpJosToDel.size());
}
}
I do have code coverage for the section of my Trigger -
for(Contact c : accountContacts){
SellerContactxProperty__c scxp = c.SellerContactxProperty__r;
junObjsToDel.add(scxp);
}
delete junObjsToDel;
which suggests that the deletion of the SellerxProperty__c
is triggering the deletion of the SellerContactxProperty__c
records.
Obviously I should be searching for a list of SellerContactxProperty__c
, checking that they have been created and then that no longer exist at the end of my test but the principle's the same.