+ Start a Discussion
cduncombe44cduncombe44 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

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):

 

            
        Contact myContact = new Contact();
        myContact.firstName = 'Joe';
        myContact.lastName = 'Schmoe';
        insert myContact;
         PageReference pg = Page.intervention; 
Test.setCurrentPage(pg);
System.currentPagereference().getParameters().put('00Nd000000308OV',myContact.id);
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; }

All Answers

Starz26Starz26

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):

 

            
        Contact myContact = new Contact();
        myContact.firstName = 'Joe';
        myContact.lastName = 'Schmoe';
        insert myContact;
         PageReference pg = Page.intervention; 
Test.setCurrentPage(pg);
System.currentPagereference().getParameters().put('00Nd000000308OV',myContact.id);
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; }
This was selected as the best answer
ClintLeeClintLee

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.