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
Harold CarlsonHarold Carlson 

Custom controller test class error: "Compile Error: Constructor not defined"

I'm sure this is simple for most but I can't seem to figure out how to write a test class for a very simple custom class.  This will truly show how much I don't know how to code so I'm hoping someone will help me.

Here's my controller:

public class AccountTeamMemberController {
    
    public List<AccountTeamMember> atmList {get; set;}
    public AccountTeamMemberController(ApexPages.StandardController controller) {
        Account acc = (Account) controller.getRecord();
        
        atmList = [SELECT Id, AccountAccessLevel, AccountId, Account.Name, TeamMemberRole,UserId, User.Name FROM AccountTeamMember where AccountId =: acc.Id];
    }

}
Here's my test class that has the compile error:
@isTest
public class AccountTeamMemberControllerTest {

    @isTest
    static void testATMController() {
   
    List<AccountTeamMember> teamList=new List<AccountTeamMember>();
   
    User uUser1 = [Select Id from user where user.name = 'Jane Doe' Limit 1];
    User uUser2 = [Select Id from user where user.name = 'John Doe' Limit 1];
        
    RecordType accRT = [select id,Name from RecordType where SobjectType='Account' and Name='Customer' Limit 1];
   
    // Create the Acount
    Account aAccount = new Account();
    aAccount.Name='Test123';
    aAccount.Owner=uUser1;
    aAccount.RecordTypeID=accRT.id;
    aAccount.Account_Subtype__c='Subscription';
    aAccount.Industry='Construction';
    aAccount.Marketing_Vertical__c='Construction';
    aAccount.Website='http://www';
    aAccount.LMS_Current__c='EH&S Platform';
    aAccount.Active__c='Yes';
    aAccount.BillingCity='New York City';
    aAccount.BillingState='New York';
    
    insert aAccount;
       
    //Create the AccountTeam
    AccountTeamMember ATM1 = new AccountTeamMember();
    ATM1.AccountId = aAccount.ID;
    ATM1.TeamMemberRole = 'Owner';
    ATM1.UserId = uUser1.ID;
        
    teamList.add(ATM1);
    
    AccountTeamMember ATM2 = new AccountTeamMember();
    ATM2.AccountId = aAccount.ID;
    ATM2.TeamMemberRole = 'Co-Owner';
    ATM2.UserId = uUser2.ID;
        
    teamList.add(ATM2);
    
    if(teamList != null) { insert teamList; }
       
    Test.StartTest();
    
    PageReference pageRef = Page.Account_Team; //VF page
    Test.setCurrentPage(pageRef);
       
    AccountTeamMemberController tp = new AccountTeamMemberController();
    tp.RefreshPage();
    
    Test.StopTest();

    }
}

 
Best Answer chosen by Harold Carlson
Egor Gerasimenko 9Egor Gerasimenko 9

If you refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm you'll see this:

"If you create a constructor that takes arguments, and you still want to use a no-argument constructor, you must create your own no-argument constructor in your code. Once you create a constructor for a class, you no longer have access to the default, no-argument public constructor."

You did exactly that - created a constructor that takes arguments. Specifically 

public AccountTeamMemberController(ApexPages.StandardController controller) {

You are then trying to call a no-argument constructor on line 52 

AccountTeamMemberController tp = new AccountTeamMemberController();

You must either supply an argument of type ApexPages.StandardController when you instantiate your controller, or you must define a no-argument constructor in the controller.

All Answers

Egor Gerasimenko 9Egor Gerasimenko 9

If you refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm you'll see this:

"If you create a constructor that takes arguments, and you still want to use a no-argument constructor, you must create your own no-argument constructor in your code. Once you create a constructor for a class, you no longer have access to the default, no-argument public constructor."

You did exactly that - created a constructor that takes arguments. Specifically 

public AccountTeamMemberController(ApexPages.StandardController controller) {

You are then trying to call a no-argument constructor on line 52 

AccountTeamMemberController tp = new AccountTeamMemberController();

You must either supply an argument of type ApexPages.StandardController when you instantiate your controller, or you must define a no-argument constructor in the controller.

This was selected as the best answer
Harold CarlsonHarold Carlson
Thank you for giving me some direction Egor.  It still took me awhile to figure it out since I don't know what I'm doing but I finally got it.
 
@isTest
public class AccountTeamMemberControllerTest {

    @isTest
    static void testATMController() {
   
    List<AccountTeamMember> teamList=new List<AccountTeamMember>();
   
    User uUser1 = [Select Id from user where user.name = 'Jane Doe' Limit 1];
    User uUser2 = [Select Id from user where user.name = 'John Doe' Limit 1];
        
    RecordType accRT = [select id,Name from RecordType where SobjectType='Account' and Name='Customer' Limit 1];
   
    // Create the Acount
    Account aAccount = new Account();
    aAccount.Name='Test123';
    aAccount.Owner=uUser1;
    aAccount.RecordTypeID=accRT.id;
    aAccount.Account_Subtype__c='Subscription';
    aAccount.Industry='Construction';
    aAccount.Marketing_Vertical__c='Construction';
    aAccount.Website='http://www';
    aAccount.LMS_Current__c='EH&S Platform';
    aAccount.Active__c='Yes';
    aAccount.BillingCity='New York City';
    aAccount.BillingState='New York';
    
    insert aAccount;
       
    //Create the AccountTeam
    AccountTeamMember ATM1 = new AccountTeamMember();
    ATM1.AccountId = aAccount.ID;
    ATM1.TeamMemberRole = 'Owner';
    ATM1.UserId = uUser1.ID;
        
    teamList.add(ATM1);
    
    AccountTeamMember ATM2 = new AccountTeamMember();
    ATM2.AccountId = aAccount.ID;
    ATM2.TeamMemberRole = 'Co-Owner';
    ATM2.UserId = uUser2.ID;
        
    teamList.add(ATM2);
    
    if(teamList != null) { insert teamList; }
    
    ApexPages.StandardController sc = new ApexPages.StandardController(aAccount);
    
    Test.StartTest();
    
    PageReference pageRef = Page.Account_Team; //VF page
    Test.setCurrentPage(pageRef);
       
    AccountTeamMemberController tp = new AccountTeamMemberController(sc);
    
    Test.StopTest();

    }
}