You need to sign in to do that
Don't have an account?
Shekhar P V Palwankar
Strange behavior in TestClass. Am I missing (misunderstanding) anything here?
I have written a test class as mentioned below:
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.
@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.
All Answers
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.
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
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?