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
Ty WhitfieldTy Whitfield 

Need help writing a test for simple VF Page with controller

I have a VF page for the Account page that I needed to create in order to display Contacts that had a specific field check.  It simply queries Contacts with that field selected and displays.  When I tried to push this into production, I keep getting code coverage errors but I'm not sure how to create a test case for this.

Here is the Apex Code
public class TrainingSubscriptionListController {
    @AuraEnabled
    Public List<Contact> acctContacts {get;set;}
    public string tmp {get;set;}
    
    public void loadPage(){
        string AcctId = System.currentPageReference().getParameters().get('Id');
        acctContacts = [Select Id, Training_Subscription_User__c, Name, Email from Contact where AccountId= :AcctId and Training_Subscription_User__c = true];
        
           tmp =     '<table id="myTable"  style="width:100%; table-layout: auto;" cellspacing="0" cellpadding="10"><tr>' + 
                '<td style="padding:15px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Name</b></td>' + 
                // '<td style="width:50px; background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                //'</td>' +
                '<td style=" background-color: #0084c4 !important;background-image: none !important; color: #ffffff !important; font-size:100% !important; margin:0 !important; border:none !important;">' +
                '<b>Email</b></td>' +
                
                '</tr>';
            
            integer i = 0;           
    
            
            while(i <  acctContacts.size())
            {
                tmp = tmp + '<tr><td style="padding:15px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Name + '</a></td><td style="padding:5px;"><a href="/' + acctContacts[i].Id + '">' + acctContacts[i].Email  +  '</a></td></tr>';
                i++;
            }
         tmp = tmp + '</table>';
    }

}

I tried running it against this test but it fails as well.
@isTest
public class TestMultipleAdminCheck {
    @isTest static void TestContact() {
        Test.startTest();
        Account acct = new Account(Name='Test Account', Training_Subscription_Seats_Available__c = 1, Number_of_Support_Users__c = 1);
        insert acct;
        Contact cont1 = new Contact(FirstName='User', LastName='One',                                     
                                    AccountId=acct.Id, My_ASCI_Role__c = 'Support User');
        Contact cont2 = new Contact(FirstName='User', LastName='Two',                                     
                                    AccountId=acct.Id, My_ASCI_Role__c = 'Admin User', Training_Subscription_User__c = true);
        Contact cont3 = new Contact(FirstName='User', LastName='Three',                                     
                                    AccountId=acct.Id,    My_ASCI_Role__c = 'General User');
        Contact cont4 = new Contact(FirstName='User', LastName='Four',             
                                    AccountId=acct.Id,   My_ASCI_Role__c = 'Support User');
        insert cont1;
        insert cont2;
        insert cont3;
        insert cont4;
        
        
        Test.stopTest();
        
    }
    
}

Any help will be appreciated.
Best Answer chosen by Ty Whitfield
Steven NsubugaSteven Nsubuga
@isTest
public class TestMultipleAdminCheck {

    @isTest static void TestContact() {
        Test.startTest();
        Account acct = new Account(Name='Test Account', Training_Subscription_Seats_Available__c = 1, Number_of_Support_Users__c = 1);
        insert acct;
        Contact cont1 = new Contact(FirstName='User', LastName='One',                                     
                                    AccountId=acct.Id, My_ASCI_Role__c = 'Support User');
        Contact cont2 = new Contact(FirstName='User', LastName='Two',                                     
                                    AccountId=acct.Id, My_ASCI_Role__c = 'Admin User', Training_Subscription_User__c = true);
        Contact cont3 = new Contact(FirstName='User', LastName='Three',                                     
                                    AccountId=acct.Id,    My_ASCI_Role__c = 'General User');
        Contact cont4 = new Contact(FirstName='User', LastName='Four',             
                                    AccountId=acct.Id,   My_ASCI_Role__c = 'Support User');
        insert cont1;
        insert cont2;
        insert cont3;
        insert cont4;
        
        //ApexPages.StandardController sc = new ApexPages.StandardController(acct);
        PageReference pageRef = Page.ThePage; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(acct.Id));

        Test.setCurrentPage(pageRef);
			
        TrainingSubscriptionListController tsc = new TrainingSubscriptionListController();
        tsc.loadPage();
        Test.stopTest();
        System.assert(!String.isEmpty(tsc.tmp));
    }
    
}

All Answers

Ty WhitfieldTy Whitfield
A little more informaion on this.  The test class isn't running as the fields Training_Subscription_Seats_Available__c  and, Number_of_Support_Users__c were brought over from the sandbox but it doesn't seem that is 100% available yet.  I can see it the Fields and Relationships on the Account page but when I try to use the API, it dosn't recognize it.  I'm wondering if this is why that test is not providing the appropriate code coverage as it stops on line 5.
Steven NsubugaSteven Nsubuga
@isTest
public class TestMultipleAdminCheck {

    @isTest static void TestContact() {
        Test.startTest();
        Account acct = new Account(Name='Test Account', Training_Subscription_Seats_Available__c = 1, Number_of_Support_Users__c = 1);
        insert acct;
        Contact cont1 = new Contact(FirstName='User', LastName='One',                                     
                                    AccountId=acct.Id, My_ASCI_Role__c = 'Support User');
        Contact cont2 = new Contact(FirstName='User', LastName='Two',                                     
                                    AccountId=acct.Id, My_ASCI_Role__c = 'Admin User', Training_Subscription_User__c = true);
        Contact cont3 = new Contact(FirstName='User', LastName='Three',                                     
                                    AccountId=acct.Id,    My_ASCI_Role__c = 'General User');
        Contact cont4 = new Contact(FirstName='User', LastName='Four',             
                                    AccountId=acct.Id,   My_ASCI_Role__c = 'Support User');
        insert cont1;
        insert cont2;
        insert cont3;
        insert cont4;
        
        //ApexPages.StandardController sc = new ApexPages.StandardController(acct);
        PageReference pageRef = Page.ThePage; // Add your VF page Name here
        pageRef.getParameters().put('id', String.valueOf(acct.Id));

        Test.setCurrentPage(pageRef);
			
        TrainingSubscriptionListController tsc = new TrainingSubscriptionListController();
        tsc.loadPage();
        Test.stopTest();
        System.assert(!String.isEmpty(tsc.tmp));
    }
    
}
This was selected as the best answer
Ty WhitfieldTy Whitfield
Thanks Steven that worked.  

Looking at what you did, I understand referencing and loading the VF page.  I don't understand why you had to create an instance of the controller when the controller is on the page already.  If you can explain why, I would appreciate.  Again, thanks for your help.
Steven NsubugaSteven Nsubuga
My pleasure Ty. I needed an instance of the controller to access the controller method.
There is no other way to access the controller, ideally it would be done via the PageReference but that cannot be done at the moment.
See PageReference methods here:
 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm