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
Jon GradmanJon Gradman 

create test class for visualforce controller - please help and eager to learn

Hi,

I'm pretty new to Visualforce, although I have coding experience in other languages.  I've never built a test class for a custom controller before, and looking for some guidance.  Here is the controller code:
 
public class ParentCntacts {
    
    public List<Account> accts {get;set;}
    public Account thisAcct {get;set;}

    
    public ParentCntacts() {
    thisAcct = [Select id, Name, ParentId FROM Account WHERE id = :ApexPages.currentPage().getParameters().get('id')];
    }
    
    public List<Account> getCon() {
        accts = [SELECT id, Name, ParentId, (SELECT id, Name, Title, Main_CSM_Contact__c, Decision_Maker__c, 
            No_Longer_At_Company__c, HasOptedOutOfEmail, Role_Contact_updated__c, Email, Phone, LinkedIn_Profile__c FROM Contacts) FROM Account WHERE Id = :thisAcct.ParentId];

        return accts;
    }
}

Visualforce markup is:
<apex:page controller="ParentCntacts" >
    <apex:pageBlock >
        <apex:repeat value="{!con}" var="a">
            <apex:pageBlockSection ><b>{!a.Name}</b></apex:pageBlockSection>
            <apex:pageblockTable value="{!a.Contacts}" var="c">
                <apex:column headerValue="Contact Name">
                    <apex:outputLink id="link" value="https://cs54.salesforce.com/{!c.Id}" target="_blank" >{!c.Name}</apex:outputLink>
                </apex:column>
                <apex:column value="{!c.Title}" />
                <apex:column value="{!c.Main_CSM_Contact__c}" />
                <apex:column value="{!c.Decision_Maker__c}" />
                <apex:column value="{!c.No_Longer_At_Company__c}" />
                <apex:column value="{!c.HasOptedOutOfEmail}" />
                <apex:column value="{!c.Role_Contact_updated__c}" />
                <apex:column value="{!c.Email}" />
                <apex:column value="{!c.Phone}" />
                <apex:column value="{!c.LinkedIn_Profile__c}" />
            </apex:pageblockTable>
        </apex:repeat>
    </apex:pageBlock>
</apex:page>

The page gets put on the layout and shows contacts on a Parent Account record (shown on a child record).

Any help greatly appreciated, thanks in advance.
Best Answer chosen by Jon Gradman
Harish RamachandruniHarish Ramachandruni

@isTest 
private class ParentCntacts__Test{ 
static testMethod void testcontro() { 


Account Acc = new Account();
Acc.name = 'harish' ;
// add which data your using in controller and required in your org
insert acc ;
ApexPages.currentPage().getParameters().put('id',acc.id)

ParentCntacts pr = new ParentCntacts();
pr.getCon() ;
}
}

 

You can Use Above test class from your controller . Ask me Any Issue .



Regards , 
Harish.R.
 

All Answers

Harish RamachandruniHarish Ramachandruni

@isTest 
private class ParentCntacts__Test{ 
static testMethod void testcontro() { 


Account Acc = new Account();
Acc.name = 'harish' ;
// add which data your using in controller and required in your org
insert acc ;
ApexPages.currentPage().getParameters().put('id',acc.id)

ParentCntacts pr = new ParentCntacts();
pr.getCon() ;
}
}

 

You can Use Above test class from your controller . Ask me Any Issue .



Regards , 
Harish.R.
 
This was selected as the best answer
Jon GradmanJon Gradman
Harish, worked like a charm!  Thanks for the comments very helpful in getting this to work.
Cheers,
Jon