You need to sign in to do that
Don't have an account?
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>
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
Here is the link
http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm
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.
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.