+ Start a Discussion
DManelskiDManelski 

What is the syntax for a constructor in a controller extension test class?

I've created a simple extension to disassociate a Contact from an Account, fired from a Detail Page Button.  The controller is a standard contact controller with an extension to add a related record to a Contact to Org. junction object, which creates a former employment record.  The button brings up a VF page that asks for a new Account and employment end date.  Upon clicking save, the contact is associated with the new account using the standard controller and the extension takes care of creating the new contact to org. former employment record. 

The extension and VF page works great, no problems, but I'm having trouble determining the syntax for the test.  Any help would be greatly appreciated. The error I'm receiving: Save error: Constructor not defined: [ONEN_Job_Change].<Constructor>()  

Here's my code:

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;
    }

}


public class ONEN_Test_Job_Change {
 
 static testMethod void ONEN_Test_Changing_The_Job() {  
  
  Test.setCurrentPageReference(Page.Job_Change);
  
  ONEN_Job_Change ext = new ONEN_Job_Change();
  
  Id FirstContactId;
  Id AccountId1;
  Id AccountId2;   
  
  //create first account
  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;
  
  Contact createdFirstContact = [select Account.Name from Contact where Id =:firstContact.id];
  
  System.assertEquals('XYZ Organization',createdFirstContact.Account.Name);
  
  //create second account
  Account newAccount2 = new Account (
   name='ABC Organization'
  );
  insert newAccount2;
  
  AccountId2 = newAccount2.id; 
  
  
...more code here...  
 }
}

 



Message Edited by DManelski on 07-01-2008 03:55 PM
Ron HessRon Hess
here is an example, you will have to construct an object of the type you are extending, then pass that object in to the standard controller constructor, then take the resulting object (controller) and pass this to your constructor

from one of my test methods, the controller extension class name here is sheetCon

Code:
        sheet__c sh = new sheet__c(name ='foo');
        insert sh;
        ApexPages.StandardController stc = new ApexPages.StandardController( sh);
        sheetcon s = new sheetcon(stc);
        s.querytable();

 

DManelskiDManelski
Thanks Ron, very helpful!

The good news is that my Test now saves without errors. The bad news is that my test is failing.  I've looked far and wide for how to actually set a new value into a custom controller or extension and then use the save method from the extension.  Alas, I can't find this information anywhere.  Here's my updated test:

Code:
public class ONEN_Test_Job_Change {

static testMethod void ONEN_Test_Changing_The_Job() {

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


}
}

The code that I'm having trouble with is setting the new account ID to save and update the contact record (in my extension, really as part of the standard controller), displayed in bold in the code above.  How do I set this new ID to be saved by the controller/extension?

Thanks in advance.
 



Message Edited by DManelski on 07-01-2008 06:18 PM