Main Content

matlab.mock.TestCase Class

Namespace: matlab.mock
Superclasses: matlab.unittest.TestCase

Class for writing tests with mocking framework

Description

The matlab.mock.TestCase class lets you write tests that use the mocking framework. Because the matlab.mock.TestCase class derives from the matlab.unittest.TestCase class, your tests have access to the features of the unit testing framework, such as qualifications, fixtures, and plugins. For more information about mocking dependencies in tests, see Create Mock Object.

The matlab.mock.TestCase class is a handle class.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Creation

In most cases, you are not required to create an instance of the matlab.mock.TestCase class directly. The mocking framework automatically creates matlab.mock.TestCase instances when running the tests.

To create a matlab.mock.TestCase instance for interactive, command-line testing, use the forInteractiveUse static method.

Methods

expand all

Examples

collapse all

Create a mock and test interactions with the mock by using the matlab.mock.TestCase class.

First, import the classes used in this example.

import matlab.mock.TestCase
import matlab.unittest.constraints.IsLessThan

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create a mock with two methods for a bank account class.

[mock,behavior] = testCase.createMock("AddedMethods",["deposit" "isOpen"]);

Set up the behavior. Throw an error when the deposit mock method is called with a negative input.

testCase.throwExceptionWhen(behavior.deposit(IsLessThan(0)), ...
    MException("Account:deposit:Negative", ...
    "Deposit amount must be positive."))

Use the mock to call deposit with a positive input.

mock.deposit(100)

Verify that the deposit method was called with the specified input.

testCase.verifyCalled(behavior.deposit(100))
Verification passed.

Test if the method was called with an input value of 50. The test fails.

testCase.verifyCalled(behavior.deposit(50))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyCalled failed.
    --> Method 'deposit' was not called with the specified signature.
    --> All observed method call(s) with any signature are:
            deposit([1×1 matlab.mock.classes.Mock], 100)
    
    Specified method call:
    MethodCallBehavior
        [...] = deposit(<Mock>, 50)

Verify that deposit was not called with a negative input.

testCase.verifyNotCalled(behavior.deposit(IsLessThan(0)))
Verification passed.

Test if the isOpen method was called at least once with the mock object as the only input. The test fails.

testCase.verifyCalled(withExactInputs(behavior.isOpen))
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyCalled failed.
    --> Method 'isOpen' was never called.
    
    Specified method call:
    MethodCallBehavior
        [...] = isOpen(<Mock>)

The matlab.mock.TestCase class inherits methods from the matlab.unittest.TestCase class and its superclasses. Use the inherited verifyError method to verify that calling the deposit method with a negative input results in the error you specified when you set up the behavior.

testCase.verifyError(@() mock.deposit(-10),"Account:deposit:Negative")
Verification passed.

Version History

Introduced in R2017a