Getting an Error: Constructor not defined: [MyController].(ApexPages.StandardController)

No error when changed to : MyController controller = new MyController(); but getting error on test Class:

Class.MyController.: line 6, column 1

Class.MyController.testMethod1: line 16, column 1

My Test Class

public class MyController{
    static testMethod void testMethod1() 
     Account testAccount = new Account();
     testAccount.Name='Test Account' ;
     insert testAccount;

     PageReference pageRef = Page.MyController;
     ApexPages.currentPage().getParameters().put('ParentId', testAccount.id);  

     **MyController controller = new MyController(new ApexPages.StandardController(testAccount));**

    controller.myfile.body=Blob.valueOf('Unit Test Attachment Body');
    PageReference pageRef1 = Page.Confirmation;

My Class :

   public class MyController {
    private final Account account;
    Public Attachment myfile;

      public MyController() {
        account = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

    public Account getAccount() {
        return account;

    Public Attachment getmyfile()
        myfile = new Attachment();
        return myfile;

    Public Pagereference Savedoc()
        String accid = System.currentPagereference().getParameters().get('id');
        Attachment a = new Attachment(parentId = accid, name=myfile.name, body = myfile.body);

    try {
        insert a;} 
    catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
      return null;
      PageReference p = Page.Confirmation;
      return p ;


<apex:page controller="MyController" sidebar="false" showHeader="false" tabStyle="Account" >

    <apex:define name="body">  
     <apex:panelGrid bgcolor="white" columns="1"> 

          <apex:panelGrid width="600" cellpadding="0" cellspacing="0" bgcolor="white" columns="1" styleClass="topPanelContainer"> 
              <apex:image id="theImage" value="{!$Resource.Logo}" style="width:200px;height:35px;"/>
           <apex:form >
         <apex:sectionHeader title="Upload your file"/>

     <apex:pageblock >
      <apex:pageblocksection columns="1">
            <apex:outputField value="{!Account.name}"/>
            <apex:inputFile value="{!myfile.body}" fileName="{!myfile.name}"></apex:inputFile>
      <apex:commandbutton value="Submit" action="{!Savedoc}"/>

  • 2
    What error are you getting with running the test class while calling class instance without parameters? – Raul Feb 16 '18 at 23:19
  • Would be better to have assert statements whenever you perform a operation to avoid any unexpected result. Like adding assert after account insert. – Raul Feb 16 '18 at 23:36

You are getting this error because your controller class doesn't have ApexPages.StandardController type constructor which you are trying to create in your test class. So either modify your apex class and add one constructor

public MyController(ApexPages.StandardController stdController) {
   this.account = (Account)stdController.getRecord();


and you should use standard controller in your vf page along with extension. here is the reference:


| improve this answer | |

You have defined your controller to be a custom controller rather than a controller extension. This may be what you intended and is perfectly OK although for Account, not typical

Custom controller constructor signatures look like

public MyController() {..}

StandardController extension constructor signatures look like

public MyController(ApexPages.StandardController stdController) {..}

Your testmethod is trying to use the controller extension constructor signature

MyController controller = new MyController(new ApexPages.StandardController(testAccount));

If you really want to use a custom controller, then your testmethod needs to do:

MyController controller = new MyController();

@Himanshu has the answer for standard controller extensions

| improve this answer | |
  • @cropedy Done this : MyController controller = new MyController(); But getting this Error when running the test class: Class.MyController.: line 6, column 1 Class.MyController.testMethod1: line 16, column 1 – Smurfet Feb 17 '18 at 4:19
  • @Smurfet - your comment must have been cut off as that isn't a complete error message. Be sure to edit the OP with latest code – cropredy Feb 17 '18 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.