Consider the following controller and its test class:


public class CheckController{
   public account acc {get;set;}
   public CheckController(ApexPages.standardController std){
      acc = (Account)std.getRecord();
   public void testFunc(){
      if(acc.starhunter__Active__c !=null){

Test Class:

public class test_controllerTest{
   static testmethod void m1(){
      Account a = new account(name ='testAcc');
      insert a;
      ApexPages.standardController sc = new ApexPages.standardController(a);
      CheckController c = new CheckController(sc);
      a.starhunter__Active__c ='Yes';
      //c.acc.starhunter__Active__c = 'Yes';
      delete a;

I am unable to understand the following points.

  1. Why is it that without actually updating the field 'starhunter__Active__c'in the controller, is the change getting reflected in the test class. [There is only a.starhunter__Active__c ='Yes' , but no update statement]

  2. And if the change in field is getting reflected in the test class, why is it that on deleting the account 'a' and calling the testFunc() , the record is accessible inside the controller.


To answer you first question. Salesforce uses call by reference methodology. Thus Test class and main class are referring to same account memory instance. Whatever changes you make to the allocated memory instance it would be reflected everywhere.

To answer 2nd Question. You have deleted the record from the Database, But it still exists in the memory. testFunc() is only fetching value from memory thus the value still exists. If you query the value from Database you wont find it.

| improve this answer | |
  • 1
    No view state involved in a unit test. – Adrian Larson Jan 23 '17 at 14:52

Both the test and controller are running in the same execution environment so a reference to an object modified by one will be seen by the other. The data is in memory that both access.

In addition, you are making calls to insert and delete data in the database. That is a separate execution environment only connected to your test or controller via DML (like insert and delete) where you push changes to it and via queries where you pull changes from it.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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