You need to sign in to do that
Don't have an account?
Susan Ross-Wentworth
apex test of controller extension which pulls a list of account team members
Hi,
I have a VF page which displays a list of account team members. The page uses a controller extension which get the actual list. I have a test class in which I create an account and the team members. I don't know how to actually test that the team member list is correctly displayed.
Thank you in advance!!
Here is my VF page:
<apex:page standardController="Account" extensions="AcctTeamMemberController">
<apex:form >
<apex:pageBlock title="Account Team">
<apex:pageBlockTable value="{!atmList}" var="atm">
<apex:column width="130" value="{!atm.User.Name}"/>
<apex:column value="{!atm.TeamMemberRole}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Here is my controller extension:
public class AcctTeamMemberController {
public List<AccountTeamMember> atmList {get; set;}
public AcctTeamMemberController(ApexPages.StandardController controller) {
Account acc = (Account) controller.getRecord();
atmList = [SELECT Id, AccountId, TeamMemberRole,UserId,
User.Name FROM AccountTeamMember where AccountId =: acc.Id];
}
}
Here is my test class so far:
@isTest
public class TestAcctTeamMemberController {
private static testmethod void TestATMController(){
//create an account
Account Acc = new Account(Name='New Account', Type='Client', OwnerId='005d0000001l4LXAAY');
insert Acc;
//create account team members
AccountTeamMember atm1 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Imaging System Manager',UserId='005d000000277hpAAA');
insert atm1;
AccountTeamMember atm2 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Client Service Manager',UserId='005d000000277hgAAA');
insert atm2;
AccountTeamMember atm3 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Receptionist',UserId='005d000000277hhAAA');
insert atm3;
AccountTeamMember atm4 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Relationship Manager',UserId='005d000000277haAAA');
insert atm4;
Test.startTest();
//Inform Test Class to set current page as Page where Extension is used
Test.setCurrentPage(Page.DisplayAccountTeam);
//Instantiate object of "ApexPages.StandardController" by passing object
ApexPages.StandardController stdController = new ApexPages.StandardController(Acc);
ApexPages.currentPage().getParameters().put('id', Acc.Id);
//Create Object of Controller extension by passing object of standardController
AcctTeamMemberController ext = new AcctTeamMemberController(stdController);
//I don't know what to put here to call up the list
Test.stopTest();
}
}
I have a VF page which displays a list of account team members. The page uses a controller extension which get the actual list. I have a test class in which I create an account and the team members. I don't know how to actually test that the team member list is correctly displayed.
Thank you in advance!!
Here is my VF page:
<apex:page standardController="Account" extensions="AcctTeamMemberController">
<apex:form >
<apex:pageBlock title="Account Team">
<apex:pageBlockTable value="{!atmList}" var="atm">
<apex:column width="130" value="{!atm.User.Name}"/>
<apex:column value="{!atm.TeamMemberRole}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Here is my controller extension:
public class AcctTeamMemberController {
public List<AccountTeamMember> atmList {get; set;}
public AcctTeamMemberController(ApexPages.StandardController controller) {
Account acc = (Account) controller.getRecord();
atmList = [SELECT Id, AccountId, TeamMemberRole,UserId,
User.Name FROM AccountTeamMember where AccountId =: acc.Id];
}
}
Here is my test class so far:
@isTest
public class TestAcctTeamMemberController {
private static testmethod void TestATMController(){
//create an account
Account Acc = new Account(Name='New Account', Type='Client', OwnerId='005d0000001l4LXAAY');
insert Acc;
//create account team members
AccountTeamMember atm1 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Imaging System Manager',UserId='005d000000277hpAAA');
insert atm1;
AccountTeamMember atm2 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Client Service Manager',UserId='005d000000277hgAAA');
insert atm2;
AccountTeamMember atm3 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Receptionist',UserId='005d000000277hhAAA');
insert atm3;
AccountTeamMember atm4 = new AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Relationship Manager',UserId='005d000000277haAAA');
insert atm4;
Test.startTest();
//Inform Test Class to set current page as Page where Extension is used
Test.setCurrentPage(Page.DisplayAccountTeam);
//Instantiate object of "ApexPages.StandardController" by passing object
ApexPages.StandardController stdController = new ApexPages.StandardController(Acc);
ApexPages.currentPage().getParameters().put('id', Acc.Id);
//Create Object of Controller extension by passing object of standardController
AcctTeamMemberController ext = new AcctTeamMemberController(stdController);
//I don't know what to put here to call up the list
Test.stopTest();
}
}
AcctTeamMemberController ext = new AcctTeamMemberController(stdController);
public List<AccountTeamMember> atmList {get; set;}
system.debug('size:' + ext.atmList.size() );
// assertNotEquals(Object expected, Object actual, Object msg)
System.assertNotEquals(0, ext.atmList.size(), 'List is empty');
Asserts that the first two arguments are different. If they are the same, a fatal error is returned that causes code execution to halt.
There is a risk with real ids in tests.
You surely verify that these Ids of users already exist in production and are active (sandboxes initialized after a copy from the production org).
Account Acc = new Account(Name='New Account', Type='Client', OwnerId='005d0000001l4LXAAY');
AccountTeamMember(AccountId=Acc.Id,TeamMemberRole='Receptionist',UserId='005d000000277hhAAA');
The alternative is to create completely the users by selecting the profiles by their names (no ids at all)
https://forceadventure.wordpress.com/2013/04/11/creating-test-users/
http://salesforceworld4u.blogspot.com/2016/02/how-to-create-portal-user-in-apex-class.html