You need to sign in to do that
Don't have an account?

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:
Can not seem to call the editRecord() or the setupAccountTeam() method for the test class.
Your help would be greatly appreciated.
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.
It will do the trick. Let me know if this helps :)
Thanks,
Amit Singh
All Answers
It may not be covering because you are not setting URL parameter.
Let us know if this will help you
It will do the trick. Let me know if this helps :)
Thanks,
Amit Singh
Thank you, so very much. I ran the code this morning and come up with 92% coverage.
Robert
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
AccountTeamMember is required because in below method you are perfoming DML ( Insert/update and delete)
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
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