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
Alx MrAlx Mr 

Test apex controller get method

Good day,

I need help writing tests for my get methods. I'm quite a newbie. May you give a pice of advise or a snipet, please.
the apex controller code:
public SelectOption[] getSubGroupsMulti() {  
        aGroupId = (ApexPages.currentPage().getParameters().containsKey('groupId')?                    ApexPages.currentPage().getParameters().get('groupId'):null);
        SelectOption[] lSO_subGroups   = new SelectOption[]{};  
       strSubGroups = [select Sub_Group__c from Account where Id = :aGroupId].Sub_Group__c;
        arrSubGroups = strSubGroups.split(';');
           for (integer i=0; i<arrSubGroups.size(); i++) {  
            lSO_subGroups.add(new SelectOption(arrSubGroups[i], arrSubGroups[i]));  
        }  
        return lSO_subGroups;  
    }

Visual force code
<apex:selectList value="{!aSubGroups}" size="1" multiselect="false">
          <apex:selectOptions value="{!SubGroupsMulti}"/>
</apex:selectList>
Thank you very much

 
pconpcon
To test this, you would want to set your current page an it's parameters.  Then generate the accouns you are trying to query.  Then assert that you have the expected select groups back.

Take a look over this article [1] and it should get you down the right path.  If you have any problems with writing the test, please include what you have here and we'll be happy to help you.

It's important to note that you don't specifically reference anything in your Visualforce page when testing your controller.  You call the methods directly.

[1] https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
Alx MrAlx Mr
static testMethod void myUnitTest8(){
        
        Account g1 = new Account(Name = 'Test',
        						Trader_Id__c = 'testingg1',
        						RecordTypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'Business' 
        										AND SobjectType = 'Account' AND IsActive = true limit 1].Id);
        
        insert g1;
        Contact c1 = new Contact(FirstName = 'Test',
        						LastName = 'Test',
        						AccountId = g1.Id);
        						
		insert c1;
        Account a1 = new Account(FirstName = 'Test',
        						LastName = 'Test',
        						T_Id__c = 'testing1',
        						Group_Id__c = g1.Id,
        						RecordTypeId = [SELECT Id FROM RecordType WHERE DeveloperName = 'Individual' 
        										AND SobjectType = 'Account' AND IsActive = true limit 1].Id);
        
        insert a1;
        
        Test.startTest();
				
        PageReference testPage3 = Page.vfTInfo;
        testPage3 = Page.vfTInfo;
        
        controllerTInfo controller6 = new controllerTInfo();
        String refreshPage = controller6.save().getUrl();
        System.assertEquals('/apex/failure?error=noParam', refreshPage);
        
	testPage3.getParameters().put('id',a1.Id);
	testPage3.getParameters().put('groupId',g1.Id);
	testPage3.getParameters().put('managerId',c1.Id);
	Test.setCurrentPage(testPage3);
        
        controller6.aSubgroups = 'JZ001';
        controller6.strSubGroups = 'JZ001;JZ002;JZ003';
      	controller6.getSubGroupsMulti();
                
        Test.stopTest();
    }
Gooda day, Pcon
My most recent try is posted above. By this i got code coverage of 3 lines 2-4. the 3 parameters needed to rerender the the page.
now i have no clue how to test the split of string into array, and populating the select option list.
I think a have to mention before get method i declared the following variables variable: 
public Id aManagerId {get; set;}
public Id aId {get; set;}
public Id aGroupId {get; set;}
public string aSubgroups{get;set;}
public String strSubGroups;
public  String[] arrSubGroups;

Thank you for any help.