Mock objects are still a recent feature of MATLAB and I don't think they are practical for testing classes yet. There are still quite a few features that are lacking for sufficiantly replicating object behaviour, for instance having a mock object method call assign a value to a protected property.
The solution to the above case is to create your mock without using the meta-class instance:
testCase = matlab.mock.TestCase.forInteractiveUse;
[MyClassMock, behaviour] = createMock(testCase, 'AddedProperties', "prop1", 'AddedMethods', "answer")
testCase.assignOutputsWhen(get(behaviour.prop1), 'abc')
p = MyClassMock.prop1
It's not ideal but I don't think there's any other option at this time. You can still test for access violations this way:
import matlab.mock.actions.ThrowException
when(set(behaviour.prop1),...
ThrowException(MException('MATLAB:class:SetProhibited',...
'You cannot set the read-only property ''prop1'' of MyClass.')))
Creating a mock from a meta-class only works for classes with abstract properties and methods.
Best Answer