Invoking class for test question

The syntax of invoking the class in a test eludes me still (not so on triggers!).

If my class is the simple:

public class MyOppsApprovalExt {

    public MyOppsApprovalExt(ApexPages.StandardController controller) {


   public List <Opportunity> getOppsInApproval (){ 
       return [select Name,  Business_Line__c, Accountid, State__c, Approval_Status__c  from Opportunity where ownerID =:userinfo.getuserId() and Approval_Status__c != 'New Accounts Final Approval'and Approval_Status__c != ''];


I understand to insert a user, Account, opportunity etc. but then how to invoke the class?
Do I need to process a list through the method, or a user? Please assist, thanks.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The syntax to instantiate a class, in your example is like this:

MyOppsApprovalExt myClass = new MyOppsApprovalExt();

Whenever a class is created, a constructor is called. If you don’t provide a constructor, like I did in the snippet of code above a default,empty constructor will be called.

In your example, which I believe this is where you have the problem, your constructor needs a parameter. So any time you create a class you need to provide an instance of the Standard controller. In the test you need to do something like this:

ApexPages.StandardController sc = new ApexPages.standardController(yourTestRecordHere); // i.e contact, account etc
MyOppsApprovalExt myClass = new MyOppsApprovalExt(sc);

//Call the method of the newly created class
List<Opportunity> opps = myClass.getOppsInApproval();

Have a read about Using Constructors

And now that you know the use of a constructor, I will throw in the question … Do you need the constructor for this class?

Are those methods only helper methods? If so you can declare them static and not even worry about instantiating the class you could just call them

public static List<ProcessInstanceStep> getSteps() {
      return [select id, StepStatus from ProcessInstanceStep where Stepstatus != 'Approved'];
List<Opportunity> opps = MyOppsApprovalExt.getOppsInApproval();

Hope it helps

Method 2

You can call your class directly from the test method. It will run in the context of whoever is running the test.

private class MyOppsApprovalExtTest {

    static testmethod void test_MyOppsApprovalExt() {

        MyOppsApprovalExt ext = new MyOppsApprovalExt();

        List<Opportunity> opptys = ext.getOppsInApproval();
        List<ProcessInstanceStep> steps = ext.getSteps();

        //assertions, etc...

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x