+ Start a Discussion
NJDeveloper019NJDeveloper019 

How to test a PageReference properly

I am getting my test coverage up to snuff for deployment but I have a few page references that are connected to a commandbutton.  I am running a test for this that simply checks to make sure querySelect is not null.  However I am getting an assertion failed:

 

System.Exception: Assertion Failed: Same value: null

 

Now it is clearly not null because I am setting the variable querySelect but I am thinking querySelect is not staying set because of state of a page reference.  I really don't know though and it would be great if someone could give me a little insight as to why this is not working.

 

CONTROLLER CLASS (Some of the class has been omitted)

public class searchController
{
    private List<User> users = new List<User>();
    private List<Business_Unit__c> units = new List<Business_Unit__c>();
    private List<Account> accountResults = new List<Account>();
    private List<Contact> contactResults = new List<Contact>();
    private List<Consultant__c> consultantResults = new List<Consultant__c>();
    private List<Job_Order__c> jobOrderResults = new List<Job_Order__c>();
    private List<Submit__c> submitResults = new List<Submit__c>();
    //private List<Location__c> state = new List<Location__c>();
    //private string[] unit = new string[]{};
    private List<String> unit = new List<String>{};
    public string user = null;
    public string searchType = null;
    public integer unitCount = 0;
    private string queryUnit = null;
    private string queryUser = null;
    public string querySearch = null;
    public string querySelect = null;
   
    //Account Search Variables
    private string accountName = null;
    private string accountId = null;
    private string city = null;
    private string state = null;
   
    //Contact Search Variables
    private string contactName = null;
    private string contactId = null;
    private string contactEmail = null;
   
    //Consultant Search Variables
    private string consultantName = null;
    private string consultantId = null;
    private string consultantHomeEmail = null;
    private string consultantWorkEmail = null;
    private string consultantResume = null;
   
    //Job Order Search Variables
    private string jobOrderId = null;
    private string jobOrderPositionTitle = null;
    private string jobOrderDescription = null;
    private string jobOrderStatus = null;
   
    //Submit Search Variables
    private string submitId = null;
    private string submitStatus = null;
   
    //AJAX variables
    public boolean criteriaVal = false;
    public boolean accountVal = false;
    public boolean contactVal = false;
    public boolean consultantVal = false;
    public boolean jobOrderVal = false;
    public boolean submitVal = false;
    public boolean interviewVal = false;
    public boolean resultsVal = false;
    public boolean resultsTableVal = false;
   
    //Flags
    public boolean unitFlag = false;
    public boolean unitAllFlag = false;
    public boolean userFlag = false;
    public boolean userAllFlag = false;
    public boolean searchFlag = false;
    public boolean unitDisabledFlag = false;
   
    //Error variables
    public boolean noUnitUser = false;
    public boolean noResults = false;
    public boolean disabledUnit = false;

 

    // Account Search
    public PageReference searchAccount()
    {    
        resultsVal = true;
        unitCount = unit.size();
        querySearch = '';
        
        resetUserUnitAllFlag();
        checkUserUnit();
        
        if (userFlag == false && unitFlag == false)
        {
            setBusinessUnit();
            setUser();
            
            querySelect = 'SELECT Id, Account_ID__c, Name, Phone, Industry, Website FROM Account ';
            
            // Search Criteria entered
            if (searchFlag == true)
            {
                // Business Units chosen and All Owners
                if (unitAllFlag == true && userAllFlag == false)
                {
                    querySelect = querySelect + 'WHERE ' + querySearch + 'AND ' + queryUnit + 'ORDER BY Account_ID__c DESC';    
                }
                
                // All Business Units and Owner chosen
                if (unitAllFlag == false && userAllFlag == true)
                {
                    querySelect = querySelect + 'WHERE ' + querySearch + 'AND ' + queryUser + 'ORDER BY Account_ID__c DESC';
                }
                
                // All Business Units and Owners
                if (unitAllFlag == false && userAllFlag == false)
                {
                    querySelect = querySelect + 'WHERE ' + querySearch + 'ORDER BY Account_ID__c DESC';
                }
                
                // Business Units chosen and Owner chosen
                if (unitAllFlag == true && userAllFlag == true)
                {
                    querySelect = querySelect + 'WHERE ' + querySearch + 'AND ' + queryUnit + 'AND ' + queryUser + 'ORDER BY Account_ID__c DESC';
                }
            }
            // No Search Criteria Entered
            else
            {
                // Business Units chosen and All Owners
                if (unitAllFlag == true && userAllFlag == false)
                {
                    querySelect = querySelect + 'WHERE ' + queryUnit + 'ORDER BY Account_ID__c DESC';
                }
                
                // All Business Units and Owner chosen
                if (unitAllFlag == false && userAllFlag == true)
                {
                    querySelect = querySelect + 'WHERE ' + queryUser + 'ORDER BY Account_ID__c DESC';
                }
                
                // All Business Units and Owners
                if (unitAllFlag == false && userAllFlag == false)
                {
                    querySelect = querySelect + 'ORDER BY Account_ID__c DESC';
                }
                
                // Business Units chosen and Owner chosen
                if (unitAllFlag == true && userAllFlag == true)
                {
                    querySelect = querySelect + 'WHERE ' + queryUnit + 'AND ' + queryUser + 'ORDER BY Account_ID__c DESC';
                }    
            }

            accountResults = Database.query(querySelect);
                                
            if (accountResults.size() > 0)
            {
                noResults = false;
            }
            else
            {
                noResults = true;
            }
        }
        
        searchFlag = false;
        return null;
    }

 

    private void checkUserUnit()
    {
        if (user != null)
        {
            userFlag = false;
        }
        else
        {
            userFlag = true;
        }
        
        if (unitCount > 0)
        {
            unitFlag = false;
        }
        else
        {
            unitFlag = true;
        }
    }

}

 

TEST CLASS

@isTest
private class searchTest {
   
    static testMethod void searchTest()
    {
        PageReference pageRef = Page.AdvancedSearch;
       
        Test.setCurrentPageReference(pageRef);
       
        searchController mySearchCon = new searchController();
       
        List<SelectOption> l;
        string s;
        string t = 'true';
        string a = 'test';
        Boolean b;
        PageReference p;           

       
        mySearchCon.user = 'a';
        mySearchCon.unitCount = 1;
        mySearchCon.searchAccount();
        System.assertNotEquals(mySearchCon.querySelect, null);
       
    }
   
}

PratibhPratibh

try this it should work....

 

PageReference HRPage = new Pagereference('/apex/Forward?');
HRPage.getParameters().put('hr',objHR1.Id);
Test.setCurrentPage(HRPage1);

 

 

Note:

 

Forward  is  Custom VisualForce page.

NJDeveloper019NJDeveloper019
All I want to do is step into the searchAccount() pageReference and set variables so that I can test based upon my conditional statements.  This seems to work with a normal method but as I stated above, I am clearly setting the 'querySelect' variable and it is coming back null.  I do not know how to build a test to get this accomplished.
PratibhPratibh

i have used this thing in my test class and its working fine....

 

NJDeveloper019NJDeveloper019

PageReference HRPage = new Pagereference('/apex/Forward?');
HRPage.getParameters().put('hr',objHR1.Id);
Test.setCurrentPage(HRPage1);

 

What is objHR1.Id?  Shouldn't it be Test.setCurrentPage(HRPage);?  Where did HRPage1 come from?