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.
To get started, see Create Mock Object.
Mock Object Actions
|Define return values for method called or property accessed
|Take no action (Since R2020a)
|Invoke function handle when method is called
|Return stored property value
|Store property value
|Throw exception when method is called or when property is set or accessed
Mock Object Constraints
|Constraint qualifying mock object interactions
|Constraint determining property get access
|Constraint determining method call
|Constraint determining property set interaction
Behavior Definition and Observation
|Specify mock object method behavior and qualify method calls
|Specify mock object property behavior and qualify interactions
|Specify mock property get behavior
|Specify mock object set behavior
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 Tests That Use App Testing and Mocking Frameworks
Create fully automated tests for an app by using the app testing framework and the mocking framework.