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
Susan Ross-WentworthSusan 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();

    }
}

 
Alain CabonAlain Cabon
amList is just a public property of the class AcctTeamMemberController.

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.
 
Susan Ross-WentworthSusan Ross-Wentworth
Hi Alain, Thanks for your code; after I added it my test passed completely and my controller extension now has 100% coverage in Sandbox. So then I tried to bring my VF page, extension and test class into production. They passed all the tests, but deployment failed due to 0% code coverage. Do I have to add something more to the test class so it will meet the threshold in production? Susan Ross-Wentworth Technology Manager susan@charteroakcm.com Charter Oak Capital Management Registered Investment Advisor P: (800) 646-5720 | F: (866) 289-0903 Portland | Portsmouth | Kennebunk www.charteroakcm.com Confidentiality Notice: If you are not the person intended to receive this email, please notify us and please do not make use of this email for any purpose. Rev19122015.
Alain CabonAlain Cabon
Hi Susan,

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/
 
//Create user
Profile adminProfile = [SELECT Id FROM Profile WHERE Name = 'System Administrator' Limit 1];

User user1 = new User(
 Username = 'test12345@test.com',  
 ProfileId = adminProfile.Id,
 Alias = 'test123',
 Email = 'test12345@test.com',
 EmailEncodingKey = 'UTF-8',
 LastName = 'McTesty',
 TimeZoneSidKey = 'America/Los_Angeles',
 LocaleSidKey = 'en_US',
 LanguageLocaleKey = 'en_US'
);

insert user1;

Account Acc = new Account(Name='New Account', Type='Client', OwnerId= user1.Id );

http://salesforceworld4u.blogspot.com/2016/02/how-to-create-portal-user-in-apex-class.html