Copyright (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.
Returns 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.
Returns 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.
Validates 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
Description
Interface 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.
global MockerV1.Arguments whenArguments(final Object argument1, final Object argument2, final Object argument3, final Object argument4, final Object argument5)
Description
Creates an argument grouping which has five arguments.
This would represent a mocked method which has five parameters.
Parameter
argument1: The expected value of the first parameter.
Parameter
argument2: The expected value of the second parameter.
Parameter
argument3: The expected value of the third parameter.
Parameter
argument4: The expected value of the fourth parameter.
Parameter
argument5: The expected value of the fifth parameter.
An 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.