2

Hello Folks,

I think all of my questions cover the approach more than the technical detail or aspect. Here is another one, this one relates to force.com unit tests!**

I know there have been significant improvements in the API v24.0 and greater regarding the unit tests but this might be a really simple use case which might help a lot of other people!

Class:

Public class DeleteRecords{
    Public void DeleteAllRecords(){
           List <sObject> ListSobject = [Select Id from sObject limit 9000];
           //DML on the List - Example Delete
           Delete listsObject;
    }
}

Test Class:

@isTest(SeeAllData=true)
    public class TestDeleteClass {    
           static testmethod void testDelete() {

            /*Approach 1 */
                 //Testing Delete
                 sObject abc = new sObject(RequiredField = 'test');
                 insert abc;
                 delete abc;

                 OR
           /*Approach 2 */
                 //Create an instance of the class and call the method
                 DeleteRecords dr = new DeleteRecord();
                 dr.DeleteAllRecords();

           }
    }

Please find the approach 1 and approach 2 both in the test class. The question is below: At times, approach would not give me code coverage, I do not know why?! Yes, the records are present in my org. But approach 2 gives me 100% code coverage. Does it really mean my code is covered?!

I also want to know if its performing the delete test, how can I know if the test records are getting deleted?

Thank you!

6

The way to test this would be as follows. Note that SeeAllData is set to false as we are inserting the records in the testmethod.

@isTest(SeeAllData=false)
public class TestDeleteClass {    
       static testmethod void testDelete() {

             sObject abc = new sObject(RequiredField = 'test');
             insert abc;

             DeleteRecords dr = new DeleteRecord();

             Test.startTest();
             dr.DeleteAllRecords();
             Test.stopTest();

             system.assert([select ID from myobject__c].IsEmpty());
       }
}

Code coverage simply means that the lines of code were executed by your test method. It doesn't mean that your code is working correctly, you have to write asserts to verify that.

2

Approach 2 is only covering the code of the class because class's method has been called in that approach. That's why , code coverage is 100%. In approach 1, no class code has been called and it is only checking insert operation, that's why code coverage is not 100%.

You can confirm deletion of record by querying for that record using that record's ID.

@isTest(SeeAllData=true)
public class TestDeleteClass {    
       static testmethod void testDelete() {

             sObject abc = new sObject(RequiredField = 'test');
             insert abc;

             DeleteRecords dr = new DeleteRecord();

             Test.startTest();
             dr.DeleteAllRecords();
             Test.stopTest();

             System.assert([Select Id from sObject where Id = abc.Id].size() == 0);

       }

}

  • Cool, thanks for the help. Btw, I have not taken any formal training for any kind of development. I tried both the approaches and approach 2 worked fine. I just wanted to clarify if I am following the best practice. Thanks for the help though. – subodhbahl Jan 16 '13 at 18:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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