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
Shekhar P V PalwankarShekhar P V Palwankar 

Strange behavior in TestClass. Am I missing (misunderstanding) anything here?

I have written a test class as mentioned below:
@isTest
public class TestClassSetupMethod 
{	
	@testSetup static void setup1()
    {
    	List<Account> lstAccount=new List<Account>();
        
		for(Integer i=0;i<2;i++)
        {
        	lstAccount.add(new Account(Name='TestAccount' + i));
        }

        insert lstAccount;
	}
	
    static testmethod void testmethod1()
    {
        Account act=[Select Id,Name From Account where name=:'TestAccount0'];
        act.Phone='7276621406';
        update act;
        System.assertEquals('7276621406', act.phone);
        Account act2=[Select Id,Name From Account where name=:'TestAccount1'];
        delete act2;
        System.assertEquals('TestAccount1', act2.Name);  
        act2.Phone='7276621444';
        // update act2; // with this line commented code runs without any error 
        System.assertEquals('7276621444', act2.phone);
    }
}

I expected to get an error at line 24 since I have deleted the Account record named 'act2' on line 23.
In case the code on line 26 (update act2;) is uncommented, I get an error for update failed (System.DmlException: Update failed. First exception on row 0 with id 0012800000byUNGAA2; first error: ENTITY_IS_DELETED, entity is deleted: [] ), which I believe is correct.
 
Any idea why it does not throw any error on assetion at line 24 in case the update act2 is commented on line 26. Any help on this would be appreciated. Thanks in advance.
Best Answer chosen by Shekhar P V Palwankar
James LoghryJames Loghry
At line 24, you're deleting the record from the Object, but you're not deleting it from memory.  Hence, why you can still access it through your Apex class, but once you interact with the database (through a SOQL query or  DML operation like you're issuing on line 26), you will see an error.

All Answers

Rohit K SethiRohit K Sethi
Hi

You are delete the account then you writting assert that is why it raise an exception;

Account act2=[Select Id,Name From Account where name=:'TestAccount1'];
 delete act2;
 System.assertEquals('TestAccount1', act2.Name);  

Similarly you are using update act2; and the account is deleted already.

Thanks.
Amit Chaudhary 8Amit Chaudhary 8
You need to query again your to check  your expected result. Like below
@isTest
public class TestClassSetupMethod 
{	
	@testSetup 
	static void setup1()
    {
    	List<Account> lstAccount=new List<Account>();
        
		for(Integer i=0;i<2;i++)
        {
        	lstAccount.add(new Account(Name='TestAccount' + i));
        }

        insert lstAccount;
	}
	
    static testmethod void testmethod1()
    {
        Account act=[Select Id,Name From Account where name=:'TestAccount0'];
        act.Phone='7276621406';
        update act;
     
		System.assertEquals('7276621406', act.phone);
        Account act2=[Select Id,Name From Account where name=:'TestAccount1'];
        delete act2;
        
        Account act2 = [Select Id,Name From Account where name=:'TestAccount1'];
		
		System.assertEquals(act2, null);  
        
		//act2.Phone='7276621444';
        // update act2; // with this line commented code runs without any error 
        System.assertEquals('7276621444', act2.phone);
    }
}
Let us know if this will help you

 
James LoghryJames Loghry
At line 24, you're deleting the record from the Object, but you're not deleting it from memory.  Hence, why you can still access it through your Apex class, but once you interact with the database (through a SOQL query or  DML operation like you're issuing on line 26), you will see an error.
This was selected as the best answer
Srinivas SSrinivas S
@Amit Chaudhary
Your suggested approach is correct but line no: 27 in your code will cause following error -
System.QueryException: List has no rows for assignment to SObject

 
Amit Chaudhary 8Amit Chaudhary 8
Yes please try below code.
@isTest
public class TestClassSetupMethod 
{	
	@testSetup 
	static void setup1()
    {
    	List<Account> lstAccount=new List<Account>();
        
		for(Integer i=0;i<2;i++)
        {
        	lstAccount.add(new Account(Name='TestAccount' + i));
        }

        insert lstAccount;
	}
	
    static testmethod void testmethod1()
    {
        Account act=[Select Id,Name From Account where name=:'TestAccount0'];
        act.Phone='7276621406';
        update act;
     
		System.assertEquals('7276621406', act.phone);
        Account act2=[Select Id,Name From Account where name=:'TestAccount1'];
        delete act2;
        
        List<Account> LstAcc = [Select Id,Name From Account where name=:'TestAccount1'];
		
		System.assertEquals(LstAcc.size(),0);  
        
		//act2.Phone='7276621444';
        // update act2; // with this line commented code runs without any error 
        System.assertEquals('7276621444', act2.phone);
    }
}

 
Shekhar P V PalwankarShekhar P V Palwankar
Hello Everyone,

Thanks for a prompt responses. The issue here is the code which I have written does not generate any error whereas I am expecting it to generate one on line 24 since the account which I am trying to access is already deleted.

@ James Loghry: Even I thought that the system is referring to cached reference of the record.

Any other views on the issue?