function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raghu Sharma 6Raghu Sharma 6 

delete a record test class is failing

I have created a custom button on account to delete current account record when some conditions are met. When I click on custom button, it calls Apex Page which in turn calls Controller extension to delete the record. I have written below test class to test it. code is working good when I try to delete the record from VF page but when I try the same in test class, record is not getting deleted. System.assertequals is failing as isdeleted is returning 'false'. What am I missing here?
My VFPage:
<apex:page StandardController="Account" Extensions="MyCtrlextn" action="{!FunctiontoDelete}">
         <apex:PageMessages />
</apex:page>

My Apex Class:
public class MyCtrlExtn
{
   private ApexPages.standardcontroller stdCtrl;

   public MyCtrlExtn(ApexPages.Standardcontroller std)
   {
      stdCtrl=std;
   }

   public PageReference FunctionToDelete()
   {
     Account Acc =(Account) stdctrl.getRecord();
      return stdCtrl.delete();
   }
}
My Test Class:

@isTest
private class testClass {    
        static Account testAcc  = new Account ();          
        testAcc.Name = 'TestAccount';
        insert testAcc;
     
  static testMethod void testDel() {
         Test.startTest();
         Test.setCurrentPage(Page.MyVFPage);
        ApexPages.StandardController sc = new ApexPages.StandardController(testAcc);
        MyCtrlExtn TE = new MyCtrlExtn(sc);
        Pagereference pr = TE.FunctionToDelete();
        Account DelAcc = [select id,isdeleted from Account where id =: testAcc.id All rows];
        System.Assertequals(DelAcc.isdeleted, true);
        Test.stopTest();
    }
   
}


 
Best Answer chosen by Raghu Sharma 6
Shashikant SharmaShashikant Sharma
Hi Laxman,

You could navigate to any other view using PageReference instance read this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_system_pagereference.htm

so you could create instance and return it.
PageReference pg = new PageReference(fill url to navigate);
return pg;

Thanks
Shashikant

All Answers

Shashikant SharmaShashikant Sharma
Hi,

Test Class Data is virtually created and is not commited to database. So when you delete it it does not reach to Recyclebin either. In order to assert your result you should do 
 
List<Account> accsAfterDelete = [select id from Account where id =: testAcc.id ];

// no record should be returned by this query so assert size to 0
System.Assertequals(accsAfterDelete.size(), 0);

Thanks
Shashikant
William TranWilliam Tran

Try selecting that account before deleting:

Account acct = [SELECT ID FROM Account where id =: testAcc.id LIMIT 1];


so you your code should look this this:
 
@isTest
private class testClass {    
        static Account testAcc  = new Account ();          
        testAcc.Name = 'TestAccount';
        insert testAcc;
     
Account acct = [SELECT ID FROM Account where id =: testAcc.id LIMIT 1];


  static testMethod void testDel() {
         Test.startTest();
         Test.setCurrentPage(Page.MyVFPage);
        ApexPages.StandardController sc = new ApexPages.StandardController(acct);
        MyCtrlExtn TE = new MyCtrlExtn(sc);
        Pagereference pr = TE.FunctionToDelete();
        Account DelAcc = [select id,isdeleted from Account where id =: acct.id];
        System.Assertequals(DelAcc.isdeleted, true);
        Test.stopTest();
    }
   
}

 
Raghu Sharma 6Raghu Sharma 6
Thats didnt help either...DelAcc.isdeleted is returning false
Shashikant SharmaShashikant Sharma
This worked fine at my end, try this it will resolve the issue
Class Code:
public class MyCtrlExtn
{
   private ApexPages.standardcontroller stdCtrl;

   public MyCtrlExtn(ApexPages.Standardcontroller std)
   {
      stdCtrl=std;
   }

   public PageReference FunctionToDelete()
   {
      Account Acc =(Account) stdctrl.getRecord();
      delete acc;
      return ApexPages.currentPage();
   }
}
VFP - remove page action
 
<apex:page StandardController="Account" Extensions="MyCtrlextn">
         <apex:PageMessages />
</apex:page>

Test Class - I have asserted both result before delete and after calling the method to delete.
@isTest
private class testClass {    
        
        static testMethod void testDel() {
            
            Account testAcc  = new Account ();  
            testAcc.Name = 'TestAccount';
            insert testAcc;
            
            List<Account> delAccsBeforeInsert = [select id from Account where id =: testAcc.id];
            System.Assertequals(1, delAccsBeforeInsert.size());
            
             Test.startTest();
             Test.setCurrentPage(Page.MyVFPage);
            ApexPages.StandardController sc = new ApexPages.StandardController(testAcc);
            MyCtrlExtn TE = new MyCtrlExtn(sc);
            Pagereference pr = TE.FunctionToDelete();
            List<Account> delAccs = [select id from Account where id =: testAcc.id];
            System.Assertequals( 0, delAccs.size());
            Test.stopTest();
        }
}

Thanks
Shashikant
Shashikant SharmaShashikant Sharma
Hi Laxman,

You could navigate to any other view using PageReference instance read this: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_system_pagereference.htm

so you could create instance and return it.
PageReference pg = new PageReference(fill url to navigate);
return pg;

Thanks
Shashikant
This was selected as the best answer