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

Why are my test variables Null?

public class CaseWizardController {
    Public String AcctSearch {get;set;}
    Public String ContactSearch {get;set;}
    Public String snSearch {get;set;}
    Public String searchText;
    Public String tempText;
    Public Account selectedAccount {get; set;}
    Public Contact selectedContact {get; set;}
    Public Asset selectedAsset {get; set;}
    Public Integer matchingAssets;
    Public String CaseInit {get; set;}
    Public CaseWizardController() {
        this.AcctSearch = ApexPages.currentPage().getParameters().get('AcctSearch');
        tempText = ApexPages.currentPage().getParameters().get('asstId');
        if (tempText == null) {    
            tempText = ApexPages.currentPage().getParameters().get('cntid');
            if (tempText == null) {   
                tempText = ApexPages.currentPage().getParameters().get('acctId');
                if (tempText == null) {   
                } else {
            } else {
                searchText = tempText;  
        } else {
    public String getCaseInit(){
     system.debug(logginglevel.INFO, 'CaseInit Method>>>' + selectedAsset.Name);
        if (selectedAsset==null) {        
         if (selectedContact==null){        
          CaseInit = '/apex/casewizard3';        
         } else {
          CaseInit = '/apex/casewizard3—cntId='+selectedContact.Id;        
        } else {
            CaseInit = '/apex/casewizard3–asstId='+selectedAsset.Id;        
       return CaseInit;        
    Public PageReference gotoPage1(String ParamValue){
     PageReference Page1 = new PageReference('/apex/casewizard1'+ParamValue);
     return Page1;
    static testMethod void testCaseWizard() { 
        CaseWizardController testWizard = new CaseWizardController();
        Asset selectedAsset=[Select Warranty_End_Date__c, Status, Ship_Date__c, Sales_Order__c, Name, Id, ContactId, AccountId, SerialNumber From Asset where serialnumber = '2' Limit 1];
        system.debug(logginglevel.INFO, 'Test Method>>>' + selectedAsset.Name);


Can someone help?  I must be missing something here.  When I run tests on the above controller, it bombs whenever I try to access the variable selectedAsset.  I can't seem to instatiate the variable within the main controller.  The test method sets the variable using a query.  I can see the variable in the debug window.  I'm calling a pagereference and passing an Asset ID to the main controller so why is selectedAsset null when I try to run getCaseInit?
In general I'm having trouble settting variables when running my test methods so most of my code that requires variables to be set are not covered.  It's not just the sObject variables.  I'm also having trouble setting parameters that are normally set on the page and passed to the controllers.
Can anyone see what I'm missing?
Please help.
Kirk F.ax361Kirk F.ax361
As Ron Hess points out in this post,

Test.setCurrentPageReference(new PageReference('Page.mergeLeadToOpp')); 

so, there's some solution for you in that.  In your test case, set your current page reference, then put your asstId, cntid, and acctId parameters.  Then your code will find the paramters, get them, and use them.