You need to sign in to do that
Don't have an account?
cduncombe44
Simple custom extension test method
I have what I believe is a very simple controller extension, and I cant seem to get any higher the 66% coverage on my test.
The page is a page for creating a new custom object (Intervention__c), this page is called from a button on a contact page. I pass the contact id as a param for this page.
I would apprecitae any help.
public class newInterventionCONT { Public String ConID = System.currentPagereference().getParameters().get('00Nd000000308OV'); public Contact myCon; public newInterventionCONT(ApexPages.StandardController controller) { myCon= [SELECT Name, Id FROM Contact WHERE Id = :System.currentPagereference().getParameters().get('00Nd000000308OV')]; } public Contact getMyCon(){ return myCon; } static testMethod void testgetConMethod() { PageReference pg = Page.intervention; Test.setCurrentPage(pg); Contact myContact = new Contact(); myContact.firstName = 'Joe'; myContact.lastName = 'Schmoe'; insert myContact; ApexPages.StandardController stanCont = new ApexPages.standardController(myContact); newInterventionCONT controller = new newInterventionCONT(stanCont); string conFirstName = controller.getmyCon().FirstName; string conLastName = controller.getmyCon().LastName; string conID = controller.getmyCon().ID; } }
Thanks,
Chris
I believe you will have to put the parameter on the pagereference before calling the method:
after you set the current page (Create the contact before this):
All Answers
I believe you will have to put the parameter on the pagereference before calling the method:
after you set the current page (Create the contact before this):
It looks like your controller is really an extension of the Contact standard controller. A Contact controller extension can be created like this, given that your Page named Intervention uses <apex:page standardController="Contact" extensions="newInterventionCont">
public class newInterventionCont {
private final Contact myCon;
public newInterventionCONT( ApexPages.StandardController controller ) {
myCont = ( Contact ) controller.getRecord();
}
public Contact getMyCon() {
return myCon;
}
static testMethod void testGetConMethod() {
Contact c = new Contact( FirstName = 'Joe', Lastame = 'Schmoe' );
insert c;
PageReference pg = Page.Intervention;
Test.setCurrentPage( pg );
ApexPages.StandardController stndController = new ApexPages.StandardController( c );
newInterventionCONT controller = new InterventionCONT( stndController );
Contact testCont = controller.getMyCon();
System.assertEquals( 'Joe', testCont.FirstName );
System.assertEquals( 'Schmoe', testCont.LastName );
}
}
Not sure if that helps since it really doesn't address the issue of creating you Intervention object.