How to delete records through unit test and wirting a test suite

I have a custom object Owner__c which has only 4 rows of record.

Here i have written test suite class

With given code below

1) 1st STEP i have fetched the values with id and checked with assert method

2) 2nd STEP i deleted the record through the list.

3) 3rd STEP i fetch records from the owner object and checked with assert statement

4) Then i want to insert record… but i couldn’t since the records has not been deleted

@isTest
private class OwnerTestController {

  static testMethod void executeOwnerSuite(){

    List<Owner__c> ownerList = null;

    Test.startTest();  // Start Executing the test

    List<Owner__c> owners = [SELECT Id, Owner_Id__c, Owner_Code__c, Name  FROM Owner__c ];

    // 1st STEP --> Here I check the list which should not be null  
    System.assert(true, owners!=null); 

    delete owners;  // 2nd STEP --> deleting the record

    owners = [SELECT Id, Owner_Id__c, Owner_Code__c, Name  FROM Owner__c ];

    // 3rd STEP --> Here I check the list is null after deleting the record     
    System.assert(true, owners==null);  

    Test.stopTest();  // Stops the test execution
  }

}

Do i make any mistake in the above code? Since the record has not been deleted. Is it right way executing a test suite?

In eclipse i’m not able to find any test result, whether it is passed or failed.

In Apex test result i find it is passed, but none of the records has not been deleted.

Kindly help me and let me know in case of any further information.

Below i have given the screen shot

enter image description here

Answer

The problem is that your test class doesn’t have any data to operate on; by default test methods cannot see your org’s data unless you specify seeAllData=true. You need to either setup test data (preferred method) or use seeAllData (use cautiously).

For more example, please read Understanding Test Data and the sections following it.

Attribution
Source : Link , Question Author : Arun , Answer Author : Mike Chale

Leave a Comment