/** * Check that all methods declared in the given interface were called. Does *not* include * methods from super interfaces. * * @return this for fluent api */ public MethodCallTester<MonitoredClass> assertAllInterfaceMethodsCalled() { List<MethodInvocation> currentInvocation = new ArrayList<>(invocations); // Debugger calling toString can mess with this for (Method method : mockClass.getDeclaredMethods()) { if (Modifier.isStatic(method.getModifiers()) || !Modifier.isPublic(method.getModifiers())) continue; boolean called = false; for (MethodInvocation invocation : currentInvocation) { if (invocation.method.equals(method.getName())) { called = true; break; } } assertTrue( "Method " + mockClass.getSimpleName() + "." + method.getName() + " was not called", called); } return this; }
/** * Create a default object of the given type. Prefers constructors with as few arguments as * possible. Creates an empty proxy for interfaces. * * @param type type to instantiate * @return default instance * @throws Exception if the default instance could not be created */ private static Object instantiateType(Class<?> type) throws Exception { if (type.isPrimitive()) return Defaults.defaultValue(type); else if (type == Void.class) return null; else if (type.isArray()) return Array.newInstance(type, 0); else if (type.isInterface()) return Proxy.newProxyInstance( type.getClassLoader(), new Class[] {type}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } }); // Take a constructor with as few params as possible Constructor constructor = type.getDeclaredConstructors()[0]; for (Constructor<?> c : type.getDeclaredConstructors()) { if (c.getParameterTypes().length < constructor.getParameterTypes().length) constructor = c; } Object[] params = new Object[constructor.getParameterTypes().length]; for (int i = 0; i < constructor.getParameterTypes().length; i++) { params[i] = instantiateType(constructor.getParameterTypes()[i]); } return constructor.newInstance(params); }
public MethodCallTester<MonitoredClass> assertMethodNotCalled( String name, Class<?>... paramTypes) { assertFalse( "Method " + mockClass.getSimpleName() + "." + name + "(" + Arrays.toString(paramTypes) + ") was called but no call was expected", wasMethodCalled(name, paramTypes)); return this; }
public MethodCallTester<MonitoredClass> assertMethodCalled( String name, Object... expectedParamValues) { assertTrue( "Method " + mockClass.getSimpleName() + "." + name + "(" + Arrays.toString(expectedParamValues) + ") was not called", wasMethodCalled(name, expectedParamValues)); return this; }
@SuppressWarnings("unused") public MethodCallTester<MonitoredClass> assertMethodNotCalled( String name, Object... expectedParamValues) { assertFalse( "Method " + mockClass.getSimpleName() + "." + name + "(" + Arrays.toString(expectedParamValues) + ") was called but no call was expected", wasMethodCalled(name, expectedParamValues)); return this; }
/** * Create {@link us.ihmc.simulationconstructionset.gui.actions.ActionsTest.MethodCallTester} with * a proxy object for the given interface that logs all method calls. * * @param interfaceClass interface to proxy * @param forwardInstance interface implementation that will be called when a call is logged (can * be null) * @param <T> interface type * @return method tester instance */ private static <T> MethodCallTester<T> createMethodTesterForInterface( Class<T> interfaceClass, final T forwardInstance) { final List<MethodInvocation> invocations = new ArrayList<>(); //noinspection unchecked T mock = (T) Proxy.newProxyInstance( interfaceClass.getClassLoader(), new Class[] {interfaceClass}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { invocations.add(new MethodInvocation(method.getName(), args)); if (forwardInstance != null) return method.invoke(forwardInstance, args); return null; } }); return new MethodCallTester<>(mock, interfaceClass, invocations); }