Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

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);

       }

}

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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