Package mockit
Interface Delegate<T>
- Type Parameters:
T
- the type of the argument to be matched, when used with thewith(Delegate)
method
public interface Delegate<T>
An empty interface to be used with the
Expectations.result
field or the
Invocations#with(Delegate) method, allowing test code to define
varying invocation results or argument matching rules, respectively.
When combined with the result
field, a test will typically assign it with an anonymous class object
implementing this interface and containing a delegate method:
new Expectations() {{
mock.doSomething(anyInt, anyString);
result = new Delegate() {
String <strong>delegate</strong>(int i, String s) {
return i > 0 ? s : "";
}
};
}};
tested.exerciseCodeUnderTest();
The delegate class (either named or anonymous) must contain exactly one non-private
instance method to
be executed when the mocked method or mocked constructor is invoked; it can contain any number of
private
or static
methods, though. The name of the delegate method can be anything. Its
parameters, however, should be the same as the parameters of the corresponding mocked method/constructor, or at least
be compatible with them. Optionally, the delegate method can have an extra parameter of type Invocation
,
provided it appears as the first one. The delegate method is also allowed to have no parameters (without
counting the optional Invocation
parameter).
When used with the result
field, the result of a delegate method execution can be any return value
compatible with the recorded method's return type, or a thrown error/exception.
When used with the with(Delegate)
method, the delegate method must return a boolean
, being
true
for a successfully matched argument or false
otherwise.
- See Also: