+ Start a Discussion
HoustonAPlusHoustonAPlus 

Help in writing test class for simple apex class

this is my first attempt at writing apex code and I am encountering a roadblock with writing test code for a basic apex class.

 

public class ExtContactRoles{
    
    private final Opportunity temp;
    private Boolean ContactRoleFound;
    
    private ApexPages.StandardController controller;
    
    public ExtContactRoles(ApexPages.StandardController stdController){
        
        ContactRoleFound = false;
        this.temp = (Opportunity)stdController.getRecord();
        for (OpportunityContactRole o:[select Id from OpportunityContactRole where OpportunityId =:temp.Id]){
            
            ContactRoleFound = true;
            
        }
        
    }
    
    
    public PageReference HasContactRole(){
        
        if(!ContactRoleFound){
            PageReference pr = new PageReference('/p/opp/ContactRoleEditUi/e?oppid='+temp.Id);
            pr.setRedirect(true);
            return pr;
        }
        else return Apexpages.currentPage();
    }

}

 

I am trying to write a test class but considering that this is an extension and this my first time writing a test class, I am not sure how to go about it.

 

this is the visual force page that accompanies it. Also the code works fine in the sandbox envoirnment. What it is supposed to do is that every time an opportunity is created, it redirects the user to add contact roles for the opporutnity. If does have an opportunity, it does not redirect. I have this visual force page embedded to the opporunity layout.

 

<apex:page standardController="Opportunity" extensions="ExtContactRoles" action="{!HasContactRole}">

</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
John De SantiagoJohn De Santiago

You will need to setup a standard controller first and then pass that to the constructor of your extension controller.

 

Example:

 

private static testMethod void testExtContactRoles {

//Set your current Page Reference

Test.setCurrentPage(Page.ExtContactRoles); //Use the name of your VF page

 

//Create your sample Opportunity

Opportunity opp = new Opportunity();

 

ApexPages.StandardController stdController = new ApexPages.StandardController(opp);

 

Test.startTest();

 

ExtContactRoles controller = new ExtContactRoles(stdController);

PageReference pageRef = controller.HasContactRole();

 

//Assert results of PageRef

System.assert(pageRef != null, 'Invalid response message goes here');

 

Test.stopTest();

}

 

Hope this helps get you started.

 

All Answers

John De SantiagoJohn De Santiago

You will need to setup a standard controller first and then pass that to the constructor of your extension controller.

 

Example:

 

private static testMethod void testExtContactRoles {

//Set your current Page Reference

Test.setCurrentPage(Page.ExtContactRoles); //Use the name of your VF page

 

//Create your sample Opportunity

Opportunity opp = new Opportunity();

 

ApexPages.StandardController stdController = new ApexPages.StandardController(opp);

 

Test.startTest();

 

ExtContactRoles controller = new ExtContactRoles(stdController);

PageReference pageRef = controller.HasContactRole();

 

//Assert results of PageRef

System.assert(pageRef != null, 'Invalid response message goes here');

 

Test.stopTest();

}

 

Hope this helps get you started.

 

This was selected as the best answer
Pradeep_NavatarPradeep_Navatar

Try out the sample code given below :

 

Opportunity opp = new Opportunity(name="testopp");

insert opp;

OpportunityContactRole opprole = new OpportunityContactRole(name='testopprole' , OpportunityId=opp.id,  ContactRoleFound = true);

insert opprole;

ApexPages.CurrentPage().getParameters().put('id', opprole.Id);

ApexPages.StandardController controller1 = new ApexPages.StandardController(opprole);

ExtContactRoles conrolecheck = new ExtContactRoles(controller1);

PageReference prf = conrolecheck.HasContactRole();

 

Hope this helps.