Main Content

Specify Mock Object Behavior

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.

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.

testCase.assignOutputsWhen(withAnyInputs(behaviorObj.findUser),"Unknown")
n = mock.findUser(1)
n = 

    "Unknown"

Specify that when the input value is 1701, the mock method returns "Jim". This behavior supersedes the return of "Unknown" for the input value of 1701 only because it was defined after that specification.

testCase.assignOutputsWhen(behaviorObj.findUser(1701),"Jim")
n = mock.findUser(1701)
n = 

    "Jim"

Specify that when the findUser method is called with only the object as input, the mock method returns "Unspecified ID". The withExactInputs method of the MethodCallBehavior object specifies that the behavior applies to a method call with the object as the only input value.

testCase.assignOutputsWhen(withExactInputs(behaviorObj.findUser), ...
    "Unspecified ID")
n = mock.findUser  % equivalent to n = findUser(mock)
n = 

    "Unspecified ID"

You can use classes in the matlab.unittest.constraints namespace to help define behavior. Specify that findUser throws an exception when it is called with an ID greater than 5000.

import matlab.unittest.constraints.IsGreaterThan
testCase.throwExceptionWhen(behaviorObj.findUser(IsGreaterThan(5000)));
n = mock.findUser(5001)
Error using
matlab.mock.internal.MockContext/createMockObject/mockMethodCallback (line 323)
The following method call was specified to throw an exception:
	   findUser([1×1 matlab.mock.classes.Mock], 5001)

You can define behavior based on the number of outputs requested in a method call. If the method call requests two output values, return "??" for the name and -1 for the ID.

testCase.assignOutputsWhen(withNargout(2, ...
    withAnyInputs(behaviorObj.findUser)),"??",-1)
[n,id] = mock.findUser(13)
n = 

    "??"


id =

    -1

Define Mock Property Behavior

When a mock property is accessed, you can specify that it returns specific or stored property values. When it is set, you can specify when the mock stores the property value. You can also define when the testing framework throws an exception for mock property set or access activities.

When defining mock property behavior, keep in mind that displaying a property value in the command window is a property access (get) operation.

Similar to defining mock method behavior, defining mock property behavior requires an instance of the PropertyBehavior class. The framework returns an instance of this class when you access a mock property. To define access behavior, use an instance of PropertyGetBehavior by calling the get method of the PropertyBehavior class. To define set behavior, use an instance of the PropertySetBehavior by calling the set or setToValue method of the PropertyBehavior class.

Specify that when the Name property is set to any value, the testing framework throws an exception.

  • The throwExceptionWhen method instructs the framework to throw an exception for a specified behavior.

  • Accessing a property on the behavior object PropertyBehavior class (behaviorObj.Name) creates a PropertyBehavior class instance.

  • The call to the set method of the PropertyBehavior class creates a PropertySetBehavior.

testCase.throwExceptionWhen(set(behaviorObj.Name))
mock.Name = "Sue";
Error using matlab.mock.internal.MockContext/createMockObject/mockPropertySetCallback (line 368)
The following property set was specified to throw an exception:
		<Mock>.Name = "Sue"

Allow the mock to store the value when the property is set to "David".

testCase.storeValueWhen(setToValue(behaviorObj.Name,"David"));
mock.Name = "David"
mock = 

  Mock with properties:

    Name: "David"
      ID: []

Define Repeating and Subsequent Behavior

The matlab.mock.TestCase methods are convenient for defining behavior. However, there is more functionality when you use a class in the matlab.mock.actions namespace instead. Using these classes, you can define behavior that repeats the same action multiple times and specify subsequent actions. To define repeating or subsequent behavior, pass an instance of a class in the matlab.mock.actions namespace to the when method of the behavior class.

Assign the value of 1138 to the ID property and then throw an exception for property access.

import matlab.mock.actions.AssignOutputs
import matlab.mock.actions.ThrowException
when(get(behaviorObj.ID),then(AssignOutputs(1138),ThrowException))
id = mock.ID
id = mock.ID
id =

        1138

Error using matlab.mock.internal.MockContext/createMockObject/mockPropertyGetCallback (line 346)
The following property access was specified to throw an exception:
   <Mock>.ID

Assign the value of 1138 and then 237 to the ID property. Then, throw an exception for property access. Each call to the then method accepts up to two actions. To specify more subsequent actions, use multiple calls to then.

when(get(behaviorObj.ID),then(AssignOutputs(1138), ...
    then(AssignOutputs(237),ThrowException)))
id = mock.ID
id = mock.ID
id = mock.ID
id =

        1138


id =

   237

Error using matlab.mock.internal.MockContext/createMockObject/mockPropertyGetCallback (line 346)
The following property access was specified to throw an exception:
   <Mock>.ID

If the object is the only input value, specify the findUser function return the value of "Phil" twice.

when(withExactInputs(behaviorObj.findUser),repeat(2,AssignOutputs("Phil")))
n = mock.findUser
n = mock.findUser
n = 

    "Phil"


n = 

    "Phil"

Call the function a third time. If you repeat an action, and do not follow it with a call to the then method, the mock continues to return the repeated value.

n = mock.findUser
n = 

    "Phil"

Define behavior for setting the value of Name. Throw an exception the first two times and then store the value.

import matlab.mock.actions.StoreValue
when(set(behaviorObj.Name),then(repeat(2,ThrowException),StoreValue))
mock.Name = "John"
Error using matlab.mock.internal.MockContext/createMockObject/mockPropertySetCallback (line 368)
The following property set was specified to throw an exception:
    <Mock>.Name = "John"
mock.Name = "Penny"
Error using matlab.mock.internal.MockContext/createMockObject/mockPropertySetCallback (line 368)
The following property set was specified to throw an exception:
    <Mock>.Name = "Penny"
mock.Name = "Tommy"
mock = 

  Mock with properties:

    Name: "Tommy"

Summary of Behaviors

BehaviorTestCase Methodmatlab.mock.Actions Class (Allows for Definition of Repeat and Subsequent Behavior)
Return specified values for method call and property access.assignOutputsWhenAssignOutputs
Return stored value when property is accessed.returnStoredValueWhenReturnStoredValue
Store value when property is set.storeValueWhenStoreValue
Throw exception when method is called or when property is set or accessed.throwExceptionWhenThrowException

See Also

Classes

Related Topics