1

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.

2

I believe the delete statement deletes the records from database, but it will not clear the list collection in memory.

so to see if the records got deleted or not, you have to query them again and then check the result size.

    List<SellerxProperty__c> sxpJosToDel = [SELECT Id, Property__c FROM SellerxProperty__c
                                             WHERE Property__c IN :propertiesForTest];

    delete sxpJosToDel;

    sxpJosToDel = [SELECT Id, Property__c FROM SellerxProperty__c
                                             WHERE Property__c IN :propertiesForTest];

    system.debug('sxpJosToDel contains ' + sxpJosToDel);
    system.assertEquals(0,sxpJosToDel.size());
1

Deleting the list of sobjects doesn't empty the List. It only delete records (move to recycle bin) from database. Even deleted record Id persist in list object record. You should query fresh record from database and then use in assert

Take the following Example:

   List<Account> accList = new List<Account>{(new Account(Name='TestIgnore'))};
   insert accList;
    System.debug('@@ '+accList.size());
   delete accList;
    System.debug('@@ DEL '+accList.size());

   accList = [SELECT id FROM Account WHERE id=:accList[0].id];
    System.debug('@@ AFT DEL QUERY '+accList.size());

Output will be:

enter image description here

  • Thanks Ashwani. I could test Vamsi's solution immediately, whereas yours would have needed adaptation which is why I've chosen to accept his. – Alex S Aug 24 '15 at 12:56
0

Remember that records in a database and sObjects in memory are not the same thing. When you delete sxpJosToDel;, you're deleting them from the database; you're not removing the in-memory sObjects from your List<SellerxProperty__c> sxpJosToDel.

If you want to check that the records have been deleted, add the IDs of the sxpJosToDel sObjects to a List<ID> sxpJosToDelIds. To assert that the records have been deleted, you can then do something like the following:

System.assertEquals(0, [SELECT Count() FROM SellerxProperty__c WHERE Id IN :sxpJosToDelIds]);
  • Thanks Rob, I've chosen Vamsi's answer included a solution which I could add straight into my code & I prefer having the option of being able to view the lists records versus a count of the list size. – Alex S Aug 24 '15 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.