Skip to main content
4 of 5
added 57 characters in body
ling
  • 454
  • 6
  • 15

StandardController.getRecord() don't work in Test?

In extension controller, when after execute StandardController.save(), i can get the newly created record Id using StandardController.getRecord(). BUT, the getRecord() method in test will not contain the Id.

is that because test is not commit? if so, how can i get the test pass?

any help would be appreciated.

here is my code:

public with sharing class accCtrl {
    
    public ApexPages.StandardController stdCtrl;
    
    public accCtrl(ApexPages.StandardController controller) {
        stdCtrl = controller;
    }
    
    public PageReference save() {

        PageReference pageRef = stdCtrl.save();  // Use save() instead of Insert to auto enforce permission check.
        system.debug(stdCtrl.getRecord());    // In actual run, can see the Id, but in test, can not.

        return pageRef;
    }
}

Test Class

@isTest
public class accCtrl_Test{
    static testMethod void test() {
        PageReference pf = Page.accTest ;
        Test.setCurrentPage(pf);
        Account acc = new Account(Name = 'Test Account');
        accCtrl controller = new accCtrl(new ApexPages.StandardController(acc));
        controller.save();
        // use newly created id to do other things.
    }
}
ling
  • 454
  • 6
  • 15