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
DavidPSGDavidPSG 

Controller Test Method cannot see object record.

I've written a controller that executes Apex code when a custom button is clicked.  I used the example in this article as my base:

http://sfdc.arrowpointe.com/2009/01/08/invoke-apex-from-a-custom-button-using-a-visualforce-page/

 

It works great and does exactly what I wanted. My version of the controller is for the Contract object.  Basically, when the button is clicked, it invokes Apex code that searches for records in a custom object called Time_Entry__c  and updates the contract# to Time_Entry__c records based on a bunch of criteria.

 

The controller:

public class CtrBnController { private final Contract c; public CtrBnController(ApexPages.StandardController stdController){ this.c = (Contract)stdController.getRecord(); } public PageReference UpdateTE(){ String theId = ApexPages.currentPage().getParameters().get('id'); if(theId == Null){ return Null; } Contract c = [select ID, AccountID, StartDate, EndDate, Contract_App__c from Contract where ID = :theID]; { for(Time_Entry__c t : [select Contract__c, Date__c from Time_Entry__c where Contract__c = Null and Case__r.AccountID = :c.AccountID and Case__r.Contract_App__c != Null and Case__r.Contract_App__c = :c.Contract_App__c]){ if(t.Date__c >= c.StartDate && t.Date__c <= c.EndDate) { t.Contract__c = c.ID; try{ Update t; } catch(DMLException D){ t.Reason__c = 'ERROR Hrs <> Work Type'; Update t; } } } } PageReference pageRef = new PageReference ('/' + theID); pageRef.setRedirect(true); return pageRef; } }

 

The problem is the test method:

static testmethod void TestCtrBn(){ Contract c = [select ID from Contract where CustomerSignedTitle = 'CtrBnController']; ApexPages.StandardController sc = new ApexPages.StandardController(c); CtrBnController controller = new CtrBnController(sc); controller.UpdateTE(); Integer t = [Select count() from Time_entry__c where Contract__c = :c.ID]; system.AssertEquals(3, t); }

 

Based on the way I've set up the test data, UpdateTE should update 3 Time Entries, so t should equal 3, but when I run the test method I get "Assertion Failed:  Expected: 3 Actual: 0"  Also, the Code Coverage results shows that none of the code in UpdateTE is being executed after

 

String theId = ApexPages.currentPage().getParameters().get('id'); if(theId == Null) { return Null; }

 

So it looks to me like the test method cannot see the Contract record.  As I said, the controller actually does work correctly from the platform when tested. 

 

Anyone see what I'm doing wrong?

 

Best Answer chosen by Admin (Salesforce Developers) 
patrospatros

I think your test method is missing the setup of the PageReference to actually pass in the 'id' parameter your code expects. Drop this in before creating your controller:

 

PageReference pageRef = Page.<YOUR_PAGE_NAME_HERE>; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',<YOUR_ID_HERE>);

 

 

All Answers

patrospatros

I think your test method is missing the setup of the PageReference to actually pass in the 'id' parameter your code expects. Drop this in before creating your controller:

 

PageReference pageRef = Page.<YOUR_PAGE_NAME_HERE>; Test.setCurrentPageReference(pageRef); ApexPages.currentPage().getParameters().put('id',<YOUR_ID_HERE>);

 

 

This was selected as the best answer
DavidPSGDavidPSG
Problem solved.  Many thanks, patros!