function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mikemelnickmikemelnick 

Constructor not defined in test class

Good Morning,

 We have been looking for a way to create a query on the contact screen to enhance the Opportunity Roles information.  We have created a VF page with tabs to display this information and both the page and extension work correctly in the sandbox. 

 

The relevant code for the VF page:

 

<apex:page standardController="Contact" extensions="OpportunityContactController" showHeader="true" tabStyle="contact" >

.

.

<apex:tab label="Donation Roles" name="OpportunityContactRoles" id="tabOppConRole">

         <apex:form >

             

              <apex:pageblock mode="edit" id="CustomList" title="Contact Roles"  >

             

                   <apex:pageBlockTable value="{!roles}" var="o" rendered="{!NOT(ISNULL(roles))}">

                       <apex:column value="{!o.OpportunityId}"/>

                       <apex:column value="{!o.Opportunity.Amount}"/>

                       <apex:column value="{!o.Opportunity.StageName}"/>

                       <apex:column value="{!o.Role}"/>

                       <apex:column value="{!o.CreatedDate}"/>

                   </apex:pageBlockTable>

                    

                   <apex:outputLabel value="No records to display" rendered="{!(ISNULL(roles))}" styleClass="noRowsHeader">

                   </apex:outputLabel>

                     

              </apex:pageblock>

                 

         </apex:form>

      </apex:tab>

 

 

The controller class:

 

public with sharing class OpportunityContactController {

 

// the soql

  private String soql {get;set;}

 

  // the collection of contact roles to display

 

  private List<OpportunityContactRole> roles;

 

  public List<OpportunityContactRole> getroles()

  {

        return roles;

  }

 

  // the contact id

  private Contact ContId;

 

  public OpportunityContactController(ApexPages.StandardController controller) {

          this.ContId= (Contact)controller.getRecord();

 

      soql = 'SELECT Role, OpportunityId, Id, ContactId, opportunity.amount, opportunity.stagename, CreatedDate From OpportunityContactRole where ContactId = \''+ContId.Id+'\'';

      queryRole();

  }

 

    // runs the actual query

  public void queryRole() {

 

    try {

      roles = Database.query(soql);

    } catch (Exception e) {

      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'SOQL Error - Please contact your SalesForce Administrator'));

    }

 

  }

}

 

When we created a test for this extension, we get a "Constructor not defined" error on the underlined piece of code below.  The test class:

@isTest

private with sharing class TEST_OpportunityContact {

 

   static testMethod void myUnitTest() {

 

        //Add test data.

        Account a = new account (name='Test Account');

        insert a;

       

        Contact t = new contact (lastname='Test Contact', accountid=a.id);

        insert t;

       

        Campaign c = new campaign (name='Test Campaign');

        insert c;

       

        Opportunity o = new opportunity (name='Test Donation', campaignid=c.id, accountid=a.id);

        insert o;

       

        OpportunityContactRole[] ocr = new OpportunityContactRole[]{};

               for(Integer i = 1; i < 11; i++){

                       OpportunityContactRole r = new OpportunityContactRole (opportunityid=o.id, contactid = t.id);

                       ocr.add(r);

               }

 

        // Instantiate a new controller  - This where we get the error.

        OpportunityContactController controller = new OpportunityContactController();

 

        controller.queryRole();

        List<OpportunityContactRole> ocrs = controller.getroles();

        System.assertEquals(10,ocrs.size());

       

    }

}

 

I have tried several different iterations of the extension class and the test but cannot seem to find one that works, any ideas?

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Prafull G.Prafull G.

Since you are using the StandardController on page so the extension having parameterized constructor. You have to do the same in case of executing the class from test methods

 

Apexpages.StandardController stdController = new Apexpages.StandardController(t); // here t is the contact instance

 

OpportunityContactController controller = new OpportunityContactController(StdController);

 

try with the above code.

 

thanks

Prafull G.

All Answers

Prafull G.Prafull G.

Since you are using the StandardController on page so the extension having parameterized constructor. You have to do the same in case of executing the class from test methods

 

Apexpages.StandardController stdController = new Apexpages.StandardController(t); // here t is the contact instance

 

OpportunityContactController controller = new OpportunityContactController(StdController);

 

try with the above code.

 

thanks

Prafull G.

This was selected as the best answer
Ankit AroraAnkit Arora

You can not use this

 

 

OpportunityContactController controller = new OpportunityContactController();

 

 

As you are using standard controller, do something like this:

 

 

ApexPages.StandardController sc = new ApexPages.standardController(new Opportunity());
// create an instance of the controller
OpportunityContactController controller = new OpportunityContactController(sc);

 

 

Thanks
Ankit Arora