0

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

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

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

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

     controller.getAccount();
     controller.getmyfile();
     controller.myfile.name='test';
    controller.myfile.body=Blob.valueOf('Unit Test Attachment Body');
    controller.Savedoc();  
    PageReference pageRef1 = Page.Confirmation;
   Test.StopTest();
}

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');
        system.debug('>>>>>AccountId'+accid);
        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'));
      system.debug('@@@@Exception'+e);
      return null;
    } 
      PageReference p = Page.Confirmation;
      return p ;
    }   
}

My VFP:

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


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

       <br/><br/>
          <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}"/>
      </apex:pageblocksection>
     </apex:pageblock>   
     </apex:form> 

               </apex:panelGrid> 
         </apex:panelGrid>
           </center>
    </apex:define>
</apex:page>
  • 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
0

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:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_extension.htm

1

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

  • @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.