Is it possible to intercept all method invocations on a mock in a generic way?
Example
Given a vendor provided class such as:
I think what you want is:
VendorObject vo = mock(VendorObject.class, new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
// 1. Check if method exists on RedirectToObject.
// 2a. If it does, call the method with the args and return the
// result.
// 2b. If it does not, throw an exception to fail the unit test.
}
});
Of course, if you want to use this approach frequently, no need for the Answer to be anonymous.
From the documentation: "It's quite advanced feature and typically you don't need it to write decent tests. However it can be helpful when working with legacy systems." Sounds like you.