Main Content

Mocking Framework

Isolate a portion of a system to test by imitating behavior of dependencies

When unit testing, you are often interested in testing a portion of a complete system isolated from dependencies. To test a portion of the system, we can use mock objects to replace the dependencies. A mock object implements at least part of the same interface as the production object, but often in a simpler, faster, more predictable, or more controllable way.

Test a component using mocked-up dependencies.

To get started, see Create Mock Object.


développer tout

matlab.mock.TestCaseTestCase to write tests with mocking framework
matlab.mock.AnyArgumentsMatch any number of arguments
matlab.mock.actions.AssignOutputsDefine return values for method called or property accessed
matlab.mock.actions.DoNothingTake no action
matlab.mock.actions.Invoke Invoke function handle when method is called
matlab.mock.actions.ReturnStoredValueReturn stored property value
matlab.mock.actions.StoreValueStore property value
matlab.mock.actions.ThrowExceptionThrow exception when method is called or when property is set or accessed
matlab.mock.constraints.Occurred Constraint qualifying mock object interactions
matlab.mock.constraints.WasAccessedConstraint determining property get access
matlab.mock.constraints.WasCalledConstraint determining method call
matlab.mock.constraints.WasSetConstraint determining property set interaction
matlab.mock.MethodCallBehaviorSpecify mock object method behavior and qualify method calls
matlab.mock.PropertyBehaviorSpecify mock object property behavior and qualify interactions
matlab.mock.PropertyGetBehaviorSpecify mock property get behavior
matlab.mock.PropertySetBehaviorSpecify mock object set behavior
getMockHistoryReturn history of mock interactions from TestCase instance
matlab.mock.InteractionHistory.forMockReturn history from mock object
matlab.mock.InteractionHistory Interface for mock object interaction history
matlab.mock.historySummary of classes representing mock object interaction history


Create Mock Object

Test a portion of a complete system in isolation using a mock.

Specify Mock Object Behavior

Define behavior for mocked methods and properties. For example, you can specify that a method returns predefined values.

Qualify Mock Object Interaction

Intercept messages sent from the component under test to the mock object. For example, you can verify that a method was called or a property was set.

Write Test That Uses App Testing and Mocking Frameworks

This example shows how to write a test that uses the app testing framework and the mocking framework.