1

I have a global class with a static method which makes post callout and I want to use Stub API, but I don't get how to do it when a method is static. I understand what to do inside mock classes, but I don't know how to call them. Here is my code:

global with sharing class UsersHelper {
global static String getAdminUser() { 
 //callout
 return  string;
}
}

@isTest
public class MockProvider implements System.StubProvider {
    public Object handleMethodCall(Object stubbedObject, String stubbedMethodName,
        Type returnType, List<Type> listOfParamTypes, List<String> listOfParamNames,
        List<Object> listOfArgs) {
        System.debug(stubbedMethodName);
        System.debug(stubbedObject);


        if (stubbedMethodName == 'getAdminUser') {
            //set response, etc
            return adminUser;
        }
        else
            return null;
    }
}


public class MockUtil {
    private MockUtil(){}

    public static MockProvider getInstance() {
        return new MockProvider();
    }

    public static Object createMock(Type typeToMock) {
        return Test.createStub(typeToMock, MockUtil.getInstance());
    }
}

1 Answer 1

5

Unfortunately, it is not possible to mock a static method using Stub API. Please take a look at the Build a Mocking Framework with the Stub API documentation.

You can’t mock the following Apex elements.

  • Static methods (including future methods)
  • Private methods
  • Properties (getters and setters)
  • Triggers
  • Inner classes
  • System types
  • Classes that implement the Batchable interface
  • Classes that have only private constructors
1
  • Oh, thanks. I read that but didnt look into limitations .
    – lemleborze
    Commented Sep 9, 2021 at 7:46

You must log in to answer this question.

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