4 package org.wamblee.test;
6 import static org.mockito.Mockito.*;
8 import java.lang.reflect.InvocationHandler;
9 import java.lang.reflect.Method;
10 import java.lang.reflect.Proxy;
13 * Resettable mock is a utility to support reset functionality for mockito.
14 * @author Erik Brakkee
18 public class ResettableMock<T> {
20 private static class MockitoInvocationHandler<T> implements
23 private Class<T> _class;
26 public MockitoInvocationHandler(Class<T> aClass) {
40 public Object invoke(Object aProxy, Method aMethod, Object[] aArgs)
42 return aMethod.invoke(_mock, aArgs);
47 * Invocation handler that delegates to the current mockito mock and allows
48 * creation of a new mock.
50 private ResettableMock.MockitoInvocationHandler<T> _handler;
53 * Proxy object to be passed to tested classes.
58 * Constructs the resettable mock.
59 * @param aType Type to mock. Must be an interface type.
61 public ResettableMock(Class<T> aType) {
62 if ( !aType.isInterface()) {
63 throw new IllegalArgumentException("Class '" + aType.getName() + "' must be an interface");
65 _handler = new MockitoInvocationHandler(aType);
66 _proxy = (T) Proxy.newProxyInstance(aType.getClassLoader(),
67 new Class[] { aType }, _handler);
71 * Resets the mock effectively forgetting all previous interactions and verifications.
78 * Gets the current mock object to pass to mockito calls.
80 * @return Mock object.
83 return _handler.getMock();
87 * Returns the proxy that your tested classes will used.
89 * @return Proxy object.