+ Start a Discussion
DManelskiDManelski 

How do I call a controller/extension method in my test?

Hello,

I'm trying to create a test for my controller extension (standard controller is Contact).  To summarize what the extension does, upon save of a VF page, one contact field is saved/updated and then the extension takes over and a new child record (to contact) is created.

The extension works great, as intended, but I'm having trouble calling the Save method from my test class.  In standard apex tests, you call a DML action like update, insert, etc. but when testing an extension, you're testing the method in the extension itself.  It follows that you need to call that method in the the test and I can't seem to figure out how.  Any help would be greatly appreciated.  Here's my code:

Controller Extension:
Code:
public class ONEN_Job_Change{
   
id ContactId;
id OldAccountId;
Contact contact = new Contact();
ONEN_Organization_2_Contact_Relationship__c relationship = new ONEN_Organization_2_Contact_Relationship__c();

    public ONEN_Job_Change(ApexPages.StandardController controller) {
        this.contact = (Contact)controller.getRecord();
        OldAccountId = contact.AccountId;
    }


    public ONEN_Organization_2_Contact_Relationship__c getRelationship() {
        
        if(relationship == null) relationship = new ONEN_Organization_2_Contact_Relationship__c();
        return relationship;
    }
    
    
    
    public PageReference save() {
        
        update contact;
        
        relationship.Organization__c = OldAccountId;
        relationship.Contact__c = contact.id;
        relationship.Type__c = 'Employee';
        
        insert relationship;
        
                
        PageReference p = new PageReference('/' + System.currentPageReference().getParameters().get('id'));
        p.setRedirect(true);
        return p;
    }

}

 And the test...

Code:
// Written by Dave Manelski, copyright (c) 2008 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class ONEN_Test_Job_Change {

static testMethod void ONEN_Test_Changing_The_Job() {
Test.setCurrentPageReference(new PageReference('Page.Job_Change'));
ONEN_Organization_2_Contact_Relationship__c relationship = new ONEN_Organization_2_Contact_Relationship__c();
Id FirstContactId;
Id AccountId1;
Id AccountId2;

Account newAccount1 = new Account (
name='XYZ Organization'
);
insert newAccount1;
AccountId1 = newAccount1.id;

//create first contact
Contact firstContact = new Contact (
FirstName='Joe',
LastName='Schmoe',
AccountId=AccountId1
);
insert firstContact;

ApexPages.StandardController sc = new ApexPages.standardController(firstContact);
ONEN_Job_Change ext = new ONEN_Job_Change(sc);
String savePage = ext.save().getUrl();




Contact createdContact = [select Account.Name from Contact where Id =:firstContact.id];

System.assertEquals('XYZ Organization',createdContact.Account.Name);


//create second account
Account newAccount2 = new Account (
name='ABC Organization'
);
insert newAccount2;

AccountId2 = newAccount2.id;

//ext = new ONEN_Job_Change(sc);
ext.firstContact.AccountId = AccountId2;
savePage = ext.save().getUrl();



System.assertEquals('ABC Organization',createdContact.Account.Name);

relationship = [select id, Contact__c, Organization__c, Type__c, End_Date__c from ONEN_Organization_2_Contact_Relationship__c where Contact__c = :FirstContactId];

System.assertEquals('XYZ Organization',relationship.Organization__c);
System.assertEquals('Employee',relationship.Type__c);


}
}

FYI, this test does not compile, I'm getting a save error that the Variable Does not Exist: firstContact on the line in bold above.

Lastly, here's my VF page for reference:
Code:
<apex:page standardController="Contact" extensions="ONEN_Job_Change">   
    <apex:sectionHeader title="Terminate Employee Relationship"/>
    <apex:form >
    <apex:pageBlock title="Choose New Organization and Previous Employment End Date">
        <apex:pageBlockSection columns="1"> 
            <apex:inputField value="{!contact.AccountId}" />
            <apex:inputField value="{!relationship.End_Date__c}"/>        
        </apex:pageBlockSection>  
            <apex:pageBlockButtons >
                    <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
        
    </apex:pageBlock>
    </apex:form>
</apex:page>


Message Edited by DManelski on 07-10-2008 05:54 PM

Message Edited by DManelski on 07-10-2008 05:54 PM
jwetzlerjwetzler
At first glance ext.firstContact is not going to compile because ext is of type ONEN_Job_Change, and there's nothing called firstContact in ONEN_Job_Change.  I think what you're trying to do is ext.getContact().AccountId?  You'll need a getContact method to return this.contact.

Try fixing that up and see if it gets you further.  If you have more problems after that I'll look at it a little more closely.
DManelskiDManelski
Is there any way that you could elaborate on where this method is called in the test and what the syntax looks like.

Thank you