Force-Framework

Mark Brennand

MockerV1

global with sharing virtual class MockerV1 extends MockerV1.Chainable
DescriptionCopyright (c) 2025 Mark Brennand, released under MIT License.

Classes providing the Mocker API.

Rather than defining fixed lists of functions and arguments to be mocked, this solution defines the arguments first and then list all the methods that expect those arguments from the class or interface being mocked.

Arguments are checked for matches using comparators. A default set of comparators are provided. Custom comparators may be added for arguments to be matched.

The default comparator for a custom object or collection of custom objects requires that only the fields set in the object to be matched are set identically in the custom object(s) passed to the mocked method call.

The Modifier interface may be used to return a value from a mocked method based on the arguments to the call. For example, this could be used to add a fake Id to custom objects being inserted.

See README for full details of the Mocker API.
AuthorMark Brennand

Methods

  any

global static Object any(final Type type)
DescriptionReturns an argument which can be used in a whenArgument call to represent a method parameter which must be of the given Type but whose value is not checked.

The parameter may be null if the types match.
Parametertype: The Type the argument is for.
ReturnsThe argument.

  anyNonNull

global static Object anyNonNull(final Type type)
DescriptionReturns an argument which can be used in a whenArgument call to represent a method parameter which must be of the given Type but whose value is not checked.

The parameter must be non null if the types match.
Parametertype: The Type the argument is for.
ReturnsThe argument.

  fakeId

global static String fakeId(final SObjectType objectType)
DescriptionGenerates a fake id for the given object type.
ParameterobjectType: The object type to have a fake Id generated for.
ReturnsThe fake id.

  of

global static MockerV1 of(final Type forType)
DescriptionCreates a new Mocker that may be used to define a mocking scenario.
ParameterforType: The class to be mocked.
ReturnsA Mocker which may be used to define a mocking scenario.

  of

global static MockerV1 of(final Factory factory)
DescriptionCreates a new Mocker that may be used to define a mocking scenario.

The Factory argument will be called to create the stubbed object.
Parameterfactory: Factory that will create the stubbed object.
ReturnsA Mocker which may be used to define a mocking scenario.

  validate

global static void validate()
DescriptionValidates that all the methods in the mocked objects have been called the required number of times.

If a method has not been called the correct number of times, an Assertion.AssertionException is thrown.

If the method has not set a called count value, it is not validated.

MockerV1.Factory

global interface Factory
DescriptionInterface allowing a user of the Mocker.of method to be called back to create the stubbed object.

As stubbed objects cannot be created by the Mocker if they are in a different namespace, this interface can be used to create the stubbed object in the caller's namespace.

Methods

  stub

Object stub(StubProvider provider)
DescriptionCrates an instance of a stubbed object. The implementation will be responsible for deciding the class of the object to stub.
Parameterprovider: The provider to use for the new stubbed object.
ReturnsThe stubbed object.

MockerV1.Chainable

global with sharing virtual class Chainable
DescriptionArgument setting class.

It is designed to be callable from both the Mocker, Arguments and Method classes. This is so all three can perform chaining of calls.

Methods

  mock

global Object mock()
DescriptionGets the mocked object for the class being mocked.
ReturnsThe mocked object.

  whenArgument

global MockerV1.Arguments whenArgument(final Object argument)
DescriptionCreates an argument grouping which has a single argument.

This would represent a mocked method which has one parameter.
Parameterargument: The expected parameter value.
ReturnsThe object instance so it can be chained.

  whenArguments

global MockerV1.Arguments whenArguments(final Object argument1, final Object argument2)
DescriptionCreates an argument grouping which has two arguments.

This would represent a mocked method which has two parameters.
Parameterargument1: The expected value of the first parameter.
Parameterargument2: The expected value of the second parameter.
ReturnsThe object instance so it can be chained.

  whenArguments

global MockerV1.Arguments whenArguments(final Object argument1, final Object argument2, final Object argument3)
DescriptionCreates an argument grouping which has three arguments.

This would represent a mocked method which has three parameters.
Parameterargument1: The expected value of the first parameter.
Parameterargument2: The expected value of the second parameter.
Parameterargument3: The expected value of the third parameter.
ReturnsThe object instance so it can be chained.

  whenArguments

global MockerV1.Arguments whenArguments(final Object argument1, final Object argument2, final Object argument3, final Object argument4)
DescriptionCreates an argument grouping which has four arguments.

This would represent a mocked method which has four parameters.
Parameterargument1: The expected value of the first parameter.
Parameterargument2: The expected value of the second parameter.
Parameterargument3: The expected value of the third parameter.
Parameterargument4: The expected value of the fourth parameter.
ReturnsThe object instance so it can be chained.

  whenArguments

global MockerV1.Arguments whenArguments(final Object argument1, final Object argument2, final Object argument3, final Object argument4, final Object argument5)
DescriptionCreates an argument grouping which has five arguments.

This would represent a mocked method which has five parameters.
Parameterargument1: The expected value of the first parameter.
Parameterargument2: The expected value of the second parameter.
Parameterargument3: The expected value of the third parameter.
Parameterargument4: The expected value of the fourth parameter.
Parameterargument5: The expected value of the fifth parameter.
ReturnsThe object instance so it can be chained.

  whenListOfArguments

global MockerV1.Arguments whenListOfArguments(final List<Object> expectedArguments)
DescriptionCreates an argument grouping which has the given arguments.
ParameterexpectedArguments: The arguments to be matched.
ReturnsThe object instance so it can be chained.

  whenNoArguments

global MockerV1.Arguments whenNoArguments()
DescriptionCreates an argument grouping which has no values.

This would represent a method with no arguments.
ReturnsThe object instance so it can be chained.

MockerV1.Modifier

global interface Modifier
DescriptionAn implementation of the Modifier interface can be added as argument to Method.returns to make changes to the method arguments when a match for the arguments is found and the registered method is called.

An example use case for this would be to add an Id to objects inserted through a call to a mocked method.

Methods

  process

Object process(List<Object> arguments)
DescriptionCalled to make changes to the return value using the arguments passed into the Method.
Parameterarguments: The arguments passed to the Method.
ReturnsThe return value.

MockerV1.Method

global with sharing class Method extends MockerV1.Chainable
DescriptionMethod definition associated with an Argument.

Methods

  called

global MockerV1.Method called(final Integer expectedCount)
DescriptionSets the number of times the mocked object is expected to be called.

The check is performed when the Mocker.validate method is called.
ParameterexpectedCount: The number of times the method is expected to be called.
ReturnsThe object instance so it can be chained.

  forMethod

global MockerV1.Method forMethod(final String methodName)
DescriptionRegisters a new method for the parent's arguments.

The method will be called when the arguments are matched.
ParametermethodName: The name of the method expected.
ReturnsThe mew method instance so it can be chained.

  returns

global MockerV1.Method returns(final Object returnValue)
DescriptionSets the return value from the method if the arguments are matched and it is called.

The return value may be set to an instance of Modifier to return a value based on the arguments passed to the mocked method call.
ParameterreturnValue: The value to be returned if the arguments are matched and this method is called.
ReturnsThe object instance so it can be chained.

  throws

global MockerV1.Method throws(final Exception throws)
DescriptionSets the Exception to be thrown from the method if the arguments are matched and it is called.
Parameterthrows: The Exception to throw if the arguments are matched and this method is called.
ReturnsThe object instance so it can be chained.

MockerV1.Arguments

global with sharing class Arguments extends MockerV1.Chainable
DescriptionClass representing a list of arguments for which mocked methods are to be registered.

Methods

  forMethod

global MockerV1.Method forMethod(final String methodName)
DescriptionRegisters a new method for the expected arguments.

The method will be called when the arguments are matched.
ParametermethodName: The name of the method expected.
ReturnsThe method instance so it can be chained.

  withComparators

global MockerV1.Arguments withComparators(final List<Comparator> comparators)
DescriptionAdds comparators to the default comparatros used when checking the arguments for equivalence.

Any additional comparators will be called before the default ones.
Parametercomparators: The additional comparators to be used when checking the expected arguments for equivalence.
ReturnsThe object instance so it can be chained.