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
Robert Davis 16Robert Davis 16 

Test PageReference Redirect Method in an Controller Extension

I have a Apex Controller Extension for a Visualforce Page with a redirect to the AccountTeamMember edit page that I can not seem to get to test in the test class. I can put the url in a Command Link to avoid testing but I really want to figure out how to write a test method:

Visualforce page portion with method in it. On an VF Page with the Account as a Standard Controller and my Controller Extension:
<apex:pageBlockTable value="{!myTeam}" var="r">
                 <apex:column >
                    <apex:commandLink value="Edit" action="{!editRecord}">
                        <apex:param name="myParmEdit" value="{!r.id}" assignTo="{!RecordID}"/>
                    </apex:commandLink>
                </apex:column>
My Controller Extension:
 
public class AcctApexControllerExt {
public List<AccountTeamMember> myTeam { get; set; }
    
    public String acctID {get; set;}
    public String ATMID {get;set;}
    private ApexPages.StandardController stdCtrl {get; set;}
    public String recordID {get; set;}
    
    public AcctApexControllerExt(ApexPages.StandardController controller) {
        stdCtrl= controller;
        setupAccountTeam();
                
    }
    public void setupAccountTeam(){
        myTeam = [SELECT Id,
                         AccountId,
                         TeamMemberRole, 
                         User.Country,
                         UserId, 
                         User.Name, 
                         User.Email, 
                         User.CompanyName, 
                         User.Title, 
                         User.FullPhotoUrl, 
                         User.SmallPhotoUrl 
                  FROM AccountTeamMember 
                  WHERE AccountId =: stdCtrl.getId() LIMIT 200];

    }    
    public PageReference addRedirect(){
        acctID = ApexPages.currentPage().getParameters().get('id');
        PageReference redir = new PageReference('/opp/salesteaminsert.jsp?retURL=%2F'+ acctID + '&id=' +acctID);
        redir.setRedirect(true);
        return redir;
    }
   public PageReference deleteRecord(){
       AccountTeamMember atm1 = new AccountTeamMember(id=ATMId);
       delete atm1;
       setupAccountTeam();
       return null;
    }
    public PageReference editRecord(){
        acctID = ApexPages.currentPage().getParameters().get('id');
        AccountTeamMember u = new AccountTeamMember(id=recordID);
        ID recID = u.id;
        PageReference redir2 = new PageReference('/acc/salesteamedit.jsp?id='+ u.Id + '&retURL='+ acctID);
        redir2.setRedirect(true);
        return redir2;
    }

}
Apex Test Class:
@isTest
public class AcctApexControllerExt_Test {
    
     static testMethod void AcctApexControllerExt_Test1(){
        Account acct = new Account (Name     = 'Test Account',
                                    Industry = 'Retail:Other') ;
        insert acct;
        Profile prof = [select id from profile where name='system Administrator'];
        User usr = new User(alias = 'usr', email='us.name@vmail.com',
                            emailencodingkey='UTF-8', lastname='lstname',
                            timezonesidkey='America/Los_Angeles',
                            languagelocalekey='en_US',
                            localesidkey='en_US', profileid = prof.Id,
                            username='testuser128@testorg.com',MobilePhone='87564231',Phone='451234789');
        insert usr;
        
        PageReference pageRef = Page.tabbedAccount;
        pageRef.getParameters().put('id', acct.id); 
        ApexPages.StandardController stdController = new ApexPages.StandardController(acct);
        AcctApexControllerExt ext = new AcctApexControllerExt(stdController);
        test.setCurrentPage(pageRef);
        ext.addRedirect();
        ext.setupAccountTeam();
        List<AccountTeamMember> results = ext.myTeam;
        system.assertEquals(results.size(), 0);
    }
}


Can not seem to call the editRecord() or the setupAccountTeam() method for the test class.

Your help would be greatly appreciated.
 
Best Answer chosen by Robert Davis 16
Amit Singh 1Amit Singh 1
Try with below code.
@isTest
public class AcctApexControllerExt_Test {
    
     static testMethod void AcctApexControllerExt_Test1(){
        Account acct = new Account (Name     = 'Test Account',
                                    Industry = 'Retail:Other') ;
        insert acct;
        Profile prof = [select id from profile where name='system Administrator'];
        User usr = new User(alias = 'usr', email='us.name@vmail.com',
                            emailencodingkey='UTF-8', lastname='lstname',
                            timezonesidkey='America/Los_Angeles',
                            languagelocalekey='en_US',
                            localesidkey='en_US', profileid = prof.Id,
                            username='testuser128@testorg.com',MobilePhone='87564231',Phone='451234789');
        insert usr;

        // Insert Account Team Member
        AccountTeamMember Teammemberad =new AccountTeamMember(); 
        Teammemberad.AccountId=acct.id; 
        Teammemberad.UserId=user.Id; 
        Teammemberad.TeamMemberRole = 'Account Manager'; 
        insert Teammemberad;

        PageReference pageRef = Page.tabbedAccount;
        test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id', acct.id); 
        ApexPages.StandardController stdController = new ApexPages.StandardController(acct);
        AcctApexControllerExt ext = new AcctApexControllerExt(stdController);
        //test.setCurrentPage(pageRef);
        ext.addRedirect();
        ext.editRecord();
        ext.deleteRecord();
        //ext.setupAccountTeam();
        List<AccountTeamMember> results = ext.myTeam;
        system.assertEquals(results.size(), 0);
    }
}

It will do the trick. Let me know if this helps :)

Thanks,
Amit Singh

 

All Answers

Andrew EchevarriaAndrew Echevarria
Hmm try adding this at the end
pageRef.getParameters().put('id', acct.Id);
pageRef = controller.editRecord();
test.setCurrentPage(pageRef);

It may not be covering because you are not setting URL parameter.
Amit Chaudhary 8Amit Chaudhary 8
please try below test class
@isTest
public class AcctApexControllerExt_Test 
{
    
    static testMethod void AcctApexControllerExt_Test1()
	{
        Account acct = new Account (Name     = 'Test Account',Industry = 'Retail:Other') ;
        insert acct;
       
		account u1=[select id,ownerid from account where id=:acct.id];
		id uid=u1.ownerid;
		
		accountteammember atm= new accountteammember();
		atm.AccountId=acct.id;
		atm.userid=u1.ownerid;
		atm.teammemberrole='Sales Assistant';
		insert atm;

		
        PageReference pageRef = Page.tabbedAccount;
        pageRef.getParameters().put('id', acct.id); 
        test.setCurrentPage(pageRef);
		
        ApexPages.StandardController stdController = new ApexPages.StandardController(acct);
        AcctApexControllerExt ext = new AcctApexControllerExt(stdController);
		ext.ATMID  = atm.id;
		
        ext.setupAccountTeam();
        List<AccountTeamMember> results = ext.myTeam;
        system.assertEquals(results.size(), 1);
		ext.addRedirect();
		ext.editRecord();
		try
		{
			ext.deleteRecord()
		}
		Catch(Exception ee)
		{}
		
    }
}

Let us know if this will help you
 
Amit Singh 1Amit Singh 1
Try with below code.
@isTest
public class AcctApexControllerExt_Test {
    
     static testMethod void AcctApexControllerExt_Test1(){
        Account acct = new Account (Name     = 'Test Account',
                                    Industry = 'Retail:Other') ;
        insert acct;
        Profile prof = [select id from profile where name='system Administrator'];
        User usr = new User(alias = 'usr', email='us.name@vmail.com',
                            emailencodingkey='UTF-8', lastname='lstname',
                            timezonesidkey='America/Los_Angeles',
                            languagelocalekey='en_US',
                            localesidkey='en_US', profileid = prof.Id,
                            username='testuser128@testorg.com',MobilePhone='87564231',Phone='451234789');
        insert usr;

        // Insert Account Team Member
        AccountTeamMember Teammemberad =new AccountTeamMember(); 
        Teammemberad.AccountId=acct.id; 
        Teammemberad.UserId=user.Id; 
        Teammemberad.TeamMemberRole = 'Account Manager'; 
        insert Teammemberad;

        PageReference pageRef = Page.tabbedAccount;
        test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id', acct.id); 
        ApexPages.StandardController stdController = new ApexPages.StandardController(acct);
        AcctApexControllerExt ext = new AcctApexControllerExt(stdController);
        //test.setCurrentPage(pageRef);
        ext.addRedirect();
        ext.editRecord();
        ext.deleteRecord();
        //ext.setupAccountTeam();
        List<AccountTeamMember> results = ext.myTeam;
        system.assertEquals(results.size(), 0);
    }
}

It will do the trick. Let me know if this helps :)

Thanks,
Amit Singh

 
This was selected as the best answer
Robert Davis 16Robert Davis 16
Amit,

Thank you, so very much. I ran the code this morning and come up with 92% coverage. 

Robert
Robert Davis 16Robert Davis 16
Amit,

Sorry to continue to ask questions after you so generously answered my question, but I am wondering why adding an Account Team Member to the database worked. How does the user id get passed to the editRecord() method.

What am I missing?

Thanks

Robert
Amit Chaudhary 8Amit Chaudhary 8
Hi Robert,

AccountTeamMember is required because in below method you are perfoming DML ( Insert/update and delete)
  1. addRedirect
  2. editRecord
  3. deleteRecord

 
Amit Singh 1Amit Singh 1
Because you are making SOQL into Account Team Member. Also, you were setting the parameters first and then after setting the Page Reference for the test class. See below example.
// What you were doing
PageReference pageRef = Page.tabbedAccount;
        pageRef.getParameters().put('id', acct.id); 
        ApexPages.StandardController stdController = new ApexPages.StandardController(acct);
        AcctApexControllerExt ext = new AcctApexControllerExt(stdController);
        test.setCurrentPage(pageRef);

// What you should do ...
PageReference pageRef = Page.tabbedAccount;
        test.setCurrentPage(pageRef);
        pageRef.getParameters().put('id', acct.id); 
        ApexPages.StandardController stdController = new ApexPages.StandardController(acct);
        AcctApexControllerExt ext = new AcctApexControllerExt(stdController);

Also, creating test data into test class is best practice.

Visit the below link for more info.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm
Robert Davis 16Robert Davis 16
Amit,

Thank you again. Generosity like yours is what makes the Salesforce Community. I will read through the above and follow your advice and understand why I should do it in this manner.

Thanks

Robert