2

I tried to write a Testclass for an Extension that is supposed to handle a replacement for a creation-page.

TestClass:

@isTest
public class VisualforcePageExtTest{
    static testMethod void validateVfPInit(){

        PageReference tpageRef = Page.VisualforcePageName;
        Test.setCurrentPage(tpageRef);

        //Testdata creation
        Account testAcc = new Account(Name = 'TestAccount', ao_Invoice_Type__c = 'Test', ax_lob__c = 'Test');
        insert testAcc;
        CustomObject testObj = new CustomObject (Name = 'TestName', 
                                                   account__c = testAcc.ID);
        insert testObj;

        Test.startTest();

        ApexPages.StandardController sc = new ApexPages.StandardController(testObj);            
        VisualforcePageExt scExt = new VisualforcePageExt(sc);

        // POSITIV TESTS
        Test.stopTest();
    }
}

I do however get the error

"Constructor not defined: [VisualforcePageExt].(ApexPages.StandardController)"

on the following line:

VisualforcePageExt scExt = new VisualforcePageExt(sc);

I also tried to use StandardSetController instead but got the error

"Constructor not defined: [ApexPages.StandardSetController].(CustomObject)"

The Extension:

public class VisualforcePageExt{

    private final CustomObject header = new CustomObject();
    public String accId = System.currentPageReference().getParameters().get('accid');

    public VisualforcePageExt(ApexPages.StandardSetController stdController) {
        if(!Test.isRunningTest()) stdController.addFields(new List<String>{'Account__c','Drucken__c','Kundennummer__c','SV_ort_f_r_FZBW__c','Zeilen_zentrieren__c','Erfasste_Zeilen__c','Land_Code__c','TP_Kopf_1__c','TP_Kopf_2__c','TP_Kopf_3__c','TP_Kopf_4__c','TP_Kopf_5__c','TP_Kopf_6__c','Schrift_1__c','Schrift_2__c','Schrift_3__c','Schrift_4__c','Schrift_5__c','Schrift_6__c','Erstellungsdatum_AS400__c'});
    }
    public CustomObject getHeader(){
        if(System.currentPageReference().getParameters().get('accid') != null){
            Account masterAcc = [SELECT BillingStreet, BillingCity, Phone FROM Account Where Id = :accId LIMIT 1];
            System.debug('Street:' + masterAcc.BillingStreet +', '+ masterAcc.BillingCity);
            header.Account__c = accId;
            header.TP_Kopf_3__c = masterAcc.BillingStreet + ', ' + masterAcc.BillingCity;
            header.TP_Kopf_4__c = masterAcc.Phone;
            header.Zeilen_zentrieren__c = true;
        }
        return header;
    }
    public PageReference save(){
        insert header;
        PageReference pg = new PageReference('/'+header.Id);
        return pg;
    }

    public PageReference redirectExistingHeader() {
        TP_Header__c[] headCount = [SELECT Name FROM TP_Header__c WHERE Account__c = :accId LIMIT 1];     
        System.Debug('Headcount:' + headCount.size());
        if(headCount.size()>0){
            PageReference pg = new PageReference('/'+headCount[0].id);
            pg.setRedirect(true);
            return pg;
        } else {
            return null;
        }
    }
}

EDIT: Added Extension

  • 4
    Can we see your actual extension constructors? – Adrian Larson May 10 '17 at 13:45
2

If you have not defined any constructors explicitly, then the empty constructor is implicitly defined, and no other constructors are allowed. That would also really make your Apex a Controller more than an Extension. Worth a read: Difference between controller and extensions

Basically, these two classes are the same as far as constructors go:

public class MyController { }

// above is same as below

public class MyController
{
    public MyController() { /*empty constructor*/ }
}

If you do indeed want to write an extension, you need to make sure you explicitly define the appropriate constructor. If you are trying to create a Detail Button/link, use StandardController. If you are trying to create a List Button, use StandardSetController:

public class MyExtension
{
    public MyExtension(ApexPages.StandardController controller)
    {
    }
}
  • 1
    Solved the problem. The Standard Construter is now defined as follows: ApexPages.StandardSetController sc = new ApexPages.StandardSetController(new List<TP_Header__c> {testHeader}); The StandardSetController was expecting a List. – Tim Werner May 10 '17 at 14:56

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.