+ Start a Discussion
Mats ErikssonMats Eriksson 

Save error: Constructor not defined

I have a VF Page where I use standardController="Case" extensions="ChangeCountryController".

 

The page and associated controller works like a charm in the sandbox. When writing the test class, I run into this error. As far as I understand, I'm using the proper method to initialise the standard controller with my extension but still get this annoying error.

 

I must have missed something fundamental. The complete error message is: Save error: Constructor not defined: [ChangeCountryController].<Constructor>(ApexPages.StandardController)

 

Below are stubs for my test class and extension.

 

//Test Class
@isTest
private class TestChangeCountryController {
  static testMethod void myUnitTest() {
    PageReference pageRef = Page.ChangeCountry;
    Test.setCurrentPage(pageRef);
    
    Case cse=new Case();
    cse.Serial_Number__c='666666666666666';
    cse.Status='New Case';
    insert cse;
        		
    ApexPages.StandardController sc = new ApexPages.StandardController(cse);
    ChangeCountryController controller = new ChangeCountryController (sc);  //<--- Complains here!
		}
}

//Controller extension
public with sharing class ChangeCountryController {
  public ApexPages.StandardController stdCtrl {get; set;}
   
  public ChangeCountryController(ApexPages.standardController std)
 	  {   
 	      Case cc=(Case) stdCtrl.getRecord();
 	  }
 }

 

 

 

Ideas anyone?

 

/Mats

 

Best Answer chosen by Admin (Salesforce Developers) 
Mats ErikssonMats Eriksson

Problem solved!

 

There were no problems with my declarations. Rather it was a matter of "the egg and the hen" (which was first?)...

 

I could not deploy my class because it was not tested and I could not deploy my Test because the class was not deployed :(

 

After much struggle I deployed Page, Class and Test Class at the same time and: Presto! It worked.

 

Cheers!

 

/Mats

All Answers

TejTej

Try This

 

private class TestChangeCountryController {

 

static testMethod void myUnitTest() { 

 

PageReference pageRef = Page.ChangeCountry;   

Test.setCurrentPage(pageRef);   

 

ApexPages.StandardController sc = new ApexPages.StandardController(cse);   

ChangeCountryController controller = new ChangeCountryController (sc);  

 

       Case cse=new Case();   

controller.cse.Serial_Number__c='666666666666666';   

controller.cse.Status='New Case'; 

 

//  insert cse;

//here call the save method as in your controller

 

controller.mysave();            

}

}

Mats ErikssonMats Eriksson

Thank you for your answer. However that won't work as cse must be defined before I reference it.

 

/Mats 

AshanAshan

I don't think you extension is correct

 

//Controller extension
public with sharing class ChangeCountryController {
public ApexPages.StandardController stdCtrl {get; set;}

public ChangeCountryController(ApexPages.standardController std)
{
Case cc=(Case) stdCtrl.getRecord();
}
}

this should be

 

//Controller extension
public with sharing class ChangeCountryController {
// public ApexPages.StandardController stdCtrl {get; set;}

public ChangeCountryController(ApexPages.standardController std)
{
Case cc=(Case) std.getRecord();
}
}

stdCtrl  will be null.

Mats ErikssonMats Eriksson

Thx Ashan but I use stdCtrl later in the constructor where I assign a value to it and as I said before, in the sandbox it works like a charm.

 

/Mats

Mats ErikssonMats Eriksson

Problem solved!

 

There were no problems with my declarations. Rather it was a matter of "the egg and the hen" (which was first?)...

 

I could not deploy my class because it was not tested and I could not deploy my Test because the class was not deployed :(

 

After much struggle I deployed Page, Class and Test Class at the same time and: Presto! It worked.

 

Cheers!

 

/Mats

This was selected as the best answer