0

How to instantiate this method in test class?

My apex class looks as below..

public static List<object> getOP(){

    User u = [SELECT Name,AccountId FROM User WHERE Id = :UserInfo.getUserId()];
    List<object> OP = [SELECT Id,Name,account__c from object 
                                    WHERE account__c = :u.AccountId];

    return OP;
}

in test method I am using below statement to instantiate above method.

getOP olrp = new getOP();

But I receive this error

error Invalid type:getOP

  • 2
    Well, getOP is a method, not a type. You can't instantiate it. You can call it like this: List<object> yourObjects = Your_Class_Name.getOP(); – Martin Lezer Jul 20 '17 at 15:46
3

You'll want to read Static and Instance Methods, Variables, and Initialization Code. Only classes can be instantiated, and only if they're not abstract. Instance methods are called using an instance of a class, and static methods are called by using the static reference ClassName.methodName.

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.