-1

I have a very simple custom controller and am trying to write a test class, but keep receiving errors...

Custom Controller

public class OpportunityController {

public List<Opportunity> opp {get;set;}

public OpportunityController() {
    opp = [SELECT Id, Name, CreatedDate, StageName, Placement__c, LDN_Company__c, Description, Job_Title__c, CloseDate, 
            NextStep, Salary__c, Future_Prospects__c, Duration__c, Training_Delivery_Site__c, Qualification_taken__c, Entry_Requirements__c, Key_Responsibilities__c, 
            Skills_Required__c, Account.Name, Account.BillingPostalCode, Account.BillingStreet, Account.BillingCity FROM Opportunity 
            WHERE (StageName = '1 - Qualifying' or StageName = '2 - Booked for CK' or StageName = '3 - Live'
           or StageName = '4 - Work Trial') AND (Placement__c = 'New Apprentice')];
        }
}

Test Method

    static testMethod void myUnitTest2() {
                test.startTest();

                Opportunity opp = new Opportunity();
                opp.Name = ' test 001';

               insert opp;

               PageReference testPage = new pagereference('/apex/Opportunities');
               ApexPages.currentPage().getParameters().put( 'id', opp.id );

               OpportunityController OpportunitiesControllerObj = new OpportunityController();
               PageReference pageref = OpportunitiesControllerObj();

               test.stopTest();
        }

Thank you

1
  • 2
    Hello and welcome to SFSE. Please take a moment to scroll through the tour and read How to Ask. Whenever you run into an error, please include the message text verbatim within your post, or you leave the community guessing and trying to fill in the gaps. Please edit this post to include the exact error message. It is also helpful to put this message on its own line staring with > to format it as a blockquote, drawing extra attention to this key detail.
    – Adrian Larson
    Commented Jul 3, 2019 at 13:15

2 Answers 2

0

You can change your Controller to this:

public class OpportunityController {

    public static List<Opportunity> action() {
        List<Opportunity> opp = [SELECT Id, Name, CreatedDate, StageName, Description, CloseDate, 
                NextStep, Account.Name, Account.BillingPostalCode, Account.BillingStreet, Account.BillingCity FROM Opportunity 
                WHERE (StageName = '1 - Qualifying' or StageName = '2 - Booked for CK' or StageName = '3 - Live'
               or StageName = '4 - Work Trial')];
        return opp;
    }
}

So, in your test class, instead of creating an instance of the controller you can actually call it's method like:

List<Opportunity opportunityList = OpportunityController.action();

Other ways, if you don't want to change your controller's code, you can get your result like this:

OpportunityController OpportunitiesControllerObj = new OpportunityController();
List<Opportunity> opps = OpportunitiesControllerObj.opp;

The constructor you wrote in your controller is called when the new object is created. So, after you have created your object, you can just get the result from the field opp you defined.

0
PageReference pageref = OpportunitiesControllerObj();

This line of code causes the error, you are trying to set as method the actual object instance.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .