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
Shri BEShri BE 

Apex Test Class Error Error: Compile Error: Constructor not defined

Hello Friends,

I got stuck in creating a Test Class. Getting error when saving the test class.
indly need your inputs to troubleshoot the error. Below is nmy Class and Test Class.
 
Class:::

public with sharing class victoriaBuildPDF {
    private final Order o;    
    
    public victoriaBuildPDF(ApexPages.StandardController standardPageController) {
        o = (Order)standardPageController.getRecord(); 
    }

    public PageReference savePdf() {
        PageReference pdf = Page.victoriaBuild;
        pdf.getParameters().put('id',o.id);
        Blob pdfBlob;

        if (!Test.isRunningTest()) {
            pdfBlob = pdf.getContent();
        } 
        else {
            pdfBlob = Blob.valueOf('PDF Test...');
        }
        
        Attachment attach = new Attachment(parentId = o.Id, Name = 'VictoriaBuild.pdf', body = pdfBlob, IsPrivate = false);
        insert attach;

        PageReference detailPage = new ApexPages.StandardController(o).view(); 
        detailPage.setRedirect(true);
        return detailPage;
        
    }
}
 
Test Class:::

@isTest
private class victoriaBuildPDF_Tests {
  private static testMethod void testSaveAndAttachPDF() {
    Account acc = new Account(Name = 'Test Account');
    insert acc;
  
    Order a = new Order(AccountId = acc.Id, EffectiveDate = system.today(), Status = 'Draft');
    insert a;

    ApexPages.currentPage().getParameters().put('Id', a.Id);
    
    victoriaBuildPDF con = new victoriaBuildPDF();
    con.savePDF();
  }
}
Error: Error: Compile Error: Constructor not defined: [victoriaBuildPDF].<Constructor>() at line 12 column 28

Thanks in Advance.
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Shri,

Greetings to you!

You are using StandardController, so you need to construct the standard controller.
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
@isTest
private class victoriaBuildPDF_Tests {
    
    private static testMethod void testSaveAndAttachPDF() {
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        Order a = new Order(AccountId = acc.Id, EffectiveDate = system.today(), Status = 'Draft');
        insert a;
        
        ApexPages.currentPage().getParameters().put('Id', a.Id);
        
        //BuildPdfTest con = new BuildPdfTest();
        
        // Construct the standard controller
        ApexPages.StandardController con = new ApexPages.StandardController(a);
        
        // create the controller
        victoriaBuildPDF ext = new victoriaBuildPDF(con);
        
        Test.startTest();
        ext.savePDF();
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas