Specify Mock Object Behavior
In this section...
“Define Mock Method Behavior” on page 33-209
“Define Mock Property Behavior” on page 33-211
“Define Repeating and Subsequent Behavior” on page 33-212
“Summary of Behaviors” on page 33-214
When you create a mock, you create an associated behavior object that controls mock
behavior. Use this object to define mock method and property behavior (stub). For more
information on creating a mock, see “Create Mock Object” on page 33-200.
The mock object is an implementation of the abstract methods and properties of the
interface specified by a superclass. You can also construct a mock without a superclass, in
which case the mock has an implicit interface.
Create a mock with an implicit interface. The interface includes Name and ID properties
and a findUser method that accepts an identifier and returns a name. While
the interface is not currently implemented, you can create a mock with it.
testCase = matlab.mock.TestCase.forInteractiveUse;
[mock,behaviorObj] = testCase.createMock('AddedProperties', ...
{'Name','ID'},'AddedMethods',{'findUser'});
Define Mock Method Behavior
You can specify that a mock method returns specific values or throws an exception in
different situations.
Specify that when the findUser method is called with any inputs, it returns "Unknown".
By default, MATLAB returns an empty array when you call the findUser method.
- The assignOutputsWhen method defines return values for the method call.
- The mocked method call (behaviorObj.findUser) implicitly creates a
MethodCallBehavior object. - The withAnyInputs method of the MethodCallBehavior object specifies that the
behavior applies to a method call with any number of inputs with any value.
Specify Mock Object Behavior