1

I have a test class for my StandardSetController and the error that is being returned is 'Constructor is not defined' for this line: 'opportunityList2Con ext = new opportunityList2Con(stdSetController)'.

@isTest

public class opportunityList2ConTestClass {



    public static final Id oppNBRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('New Business - Read Only').getRecordTypeId();

    /* Create User record */

    public static User u = TestHelper.createAdminUser('abc@xyz.com'); 

    /* Create Account record */

    public static Account a = TestHelper.createAccount(u.Id, 'Account Name - InfoYTD'); 



    public static testMethod void testOppCreation() {



        // List of Opportunities

        List<Opportunity> oppList = new List<Opportunity>();



        oppList.add(new Opportunity(

            AccountId = a.Id, OwnerId = u.Id, StageName = 'Closed Won', 

            RecordTypeId = oppNBRecordTypeId, Name = 'Opportunity NB A Test', CurrencyIsoCode = 'GBP',

            Amount = 1000, Billing_Effective_Date__c = date.today(), CloseDate = date.today()));



        oppList.add(new Opportunity(

            AccountId = a.Id, OwnerId = u.Id, StageName = 'Closed Won', 

            RecordTypeId = oppNBRecordTypeId, Name = 'Opportunity NB A Test', CurrencyIsoCode = 'GBP',

            Amount = 2000, Billing_Effective_Date__c = date.today(), CloseDate = date.today()));



        insert oppList;



        Test.startTest();



        PageReference testPage = new PageReference('/apex/opportunityList2ConVFPage');

        Test.setCurrentPage(testPage);

                        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(oppList);

                        opportunityList2Con ext = new opportunityList2Con(stdSetController);



        Test.stopTest();



    }

The extension class (condensed to show the main logic) is:

public with sharing class opportunityList2Con extends NBClosedWonYTD {

    public ApexPages.StandardSetController setCon {        
        get {
            if(setCon == null) { 

        /* Record Type Id */        
        String recordTypeIdValue = ''; 
        recordTypeIdValue = ApexPages.currentPage().getParameters().get('pv0');
        Id recordTypeIdX = Id.valueOf(recordTypeIdValue);          

        /* Calendar Months for Billing Effective Date */        
        String nb1 = '';  
        nb1 = ApexPages.currentPage().getParameters().get('pv1');
        Integer monthValue1 = Integer.valueOf(nb1);

        setCon = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Name, IsWon, convertCurrency(Amount), Owner_Region__c, CloseDate, Billing_Effective_Date__c, Owner.Name, Account.Name FROM Opportunity WHERE StageName =:stages AND Billing_Effective_Date__c = THIS_YEAR AND RecordType.Id =: recordTypeIdX AND (CALENDAR_MONTH(Billing_Effective_Date__c) =: monthValue1 ORDER BY Billing_Effective_Date__c]));

            }
            return setCon;
        } 
        set; 
    } 

    public List<Opportunity> getOpportunitiesWon() {
         setCon.setPageSize(setCon.getResultSize()); 
         return (List<Opportunity>) setCon.getRecords();
    }

    }

Can someone please advise me why this error is being returned and how I can rectify this?

3

Your test assumes your extension class has this constructor (that it doesn't have):

public with sharing class opportunityList2Con extends NBClosedWonYTD {

    public opportunityList2Con(ApexPages.StandardSetController setCon) {
        this.setCon = setCon;
    }

    ...
}

But your extension class seems designed to create its own StandardSetController based on parameters called "pv0" and "pv1". So probably best to change your test to supply those parameters and just use the default constructor:

testPage.getParameters().put('pv0', ...);
testPage.getParameters().put('pv1', ...);
Test.setCurrentPage(testPage);

opportunityList2Con ext = new opportunityList2Con();
System.assertNotEquals(null, ext.setCon);
...

(You haven't posted the NBClosedWonYTD class or the page this extension is used in; they would both potentially influence the answer here.)

| improve this answer | |

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.