StubProvider Interface

StubProvider is a callback interface that you can use as part of the Apex stub API to implement a mocking framework. Use this interface with the Test.createStub() method to create stubbed Apex objects for testing.

Namespace

System

Usage

The StubProvider interface allows you to define the behavior of a stubbed Apex class. The interface specifies a single method that requires implementing: handleMethodCall(). You specify the behavior of each method of the stubbed class in the handleMethodCall() method.

In your Apex test, you create a stubbed object using the Test.createStub() method. When you invoke methods on the stubbed object, StubProvider.handleMethodCall() is called, which performs the behavior that you’ve specified for each method.

StubProvider Methods

The following are methods for StubProvider.

handleMethodCall(stubbedObject, stubbedMethodName, returnType, listOfParamTypes, listOfParamNames, listOfArgs)

Use this method to define the behavior of each method of a stubbed class.

Signature

public Object handleMethodCall(Object stubbedObject, String stubbedMethodName, System.Type returnType, List<System.Type> listOfParamTypes, List<String> listOfParamNames, List<Object> listOfArgs)

Parameters

stubbedObject
Type: Object
The stubbed object.
stubbedMethodName
Type: String
The name of the invoked method.
returnType
Type: System.Type
The return type of the invoked method.
listOfParamTypes
Type: List<System.Type>
A list of the parameter types of the invoked method.
listOfParamNames
Type: List<String>
A list of the parameter names of the invoked method.
listOfArgs
Type: List<Object>
The actual argument values passed into this method at runtime.

Return Value

Type: Object

Usage

You can use the parameters passed into this method to identify which method on the stubbed object was invoked. Then you can define the behavior for each identified method.