I am unable to use the System.StubProvider
to mock/stub a complicated Backend class of which I have no implementation but only an interface
public interface ShopBackend {
void initialize();
Boolean hasInStock(String item);
}
Here is my test using github.com/rsoesemann/apex-mocks
@IsTest
private class MockClass_Test {
@IsTest
static void expectCallsToMockedClass() {
// Setup
MockClass mock = new MockClass(ShopBackend.class)
.expects(new Call('initialize'))
.expects(new Call('hasInStock')
.withParam('item', 'FizzBuzz')
.returning(true));
ShopBackend mockedBackend = (ShopBackend) mock.instance();
// Exercise
new Shop(mockedBackend).buy('FizzBuzz');
// Verify
System.assert(mock.verify());
}
}
which fails in this line with this error:
System.AssertException: Assertion Failed: Parameter not expected: unnamed__0
Class.Call.validate: line 33, column 1
Class.MockClass.handleMethodCall: line 37, column 1
Class.ShopBackend__sfdc_ApexStub.hasInStock: line 7, column 1
Class.Shop.buy: line 14, column 1
Class.MockClass_Test.expectCallsToMockedClass: line 18, column 1
because Parameter names are not tracked correctly (stay unnamed__0
) when mocked from an interface (not a class).
The test succeeds when I replace the interface with this empty class:
public class ShopBackend {
public void initialize() {
}
public Boolean hasInStock(String item) {
return null;
}
}
This must be a bug or an undocumented Limitation that should be added here.