private void defineParameters(Interpreter interpreter, Invocation invocation) throws EvalError { interpreter.set("$this", invocation.getInvokedObject()); for (int i = 0; i < invocation.getParameterCount(); i++) { interpreter.set("$" + i, invocation.getParameter(i)); } }
public Object invoke(Object receiver, Method method, Object[] args) throws Throwable { return mockObject.invoke(new Invocation(receiver, method, args)); } },
public boolean matches(Invocation invocation) { return allowsMoreInvocations() && objectMatcher.matches(invocation.getInvokedObject()) && methodMatcher.matches(invocation.getInvokedMethod()) && parametersMatcher.matches(invocation.getParametersAsArray()) && isInCorrectOrder(); }
@Override public Object invoke(Invocation invocation) throws Throwable { return handler.invoke(null, invocation.getInvokedMethod(), invocation.getParametersAsArray()); } };
public Object invoke(Invocation invocation) throws Throwable { if (invocation.getInvokedMethod().getDeclaringClass() == declaringType) { return invocation.applyTo(target); } else { return next.invoke(invocation); } } }
public void testCanBeConstructedFromAMethodObject() throws Exception { Invocation invocation = new Invocation(INVOKED, method, ARG_VALUES); assertSame("invoked object", INVOKED, invocation.getInvokedObject()); assertEquals("invoked method", method, invocation.getInvokedMethod()); assertEquals("name", method.getName(), invocation.getInvokedMethod().getName()); assertEquals("parameter types", Arrays.asList(method.getParameterTypes()), Arrays.asList(invocation.getInvokedMethod().getParameterTypes())); assertEquals("return type", method.getReturnType(), invocation.getInvokedMethod().getReturnType()); assertEquals("parameter count", ARG_VALUES.length, invocation.getParameterCount()); assertEquals("parameter values", Arrays.asList(ARG_VALUES), Arrays.asList(invocation.getParametersAsArray())); }
@Override public Object invoke(Invocation invocation) throws Throwable { return (Integer) invocation.getParameter(0);
public Object invoke(Invocation invocation) throws Throwable { Method method = invocation.getInvokedMethod(); if (isMethod(method, int.class, "hashCode")) { return fakeHashCode(invocation.getInvokedObject()); } else if (isMethod(method, String.class, "toString")) { return fakeToString(invocation.getInvokedObject()); } else if (isMethod(method, boolean.class, "equals", Object.class)) { return fakeEquals(invocation.getInvokedObject(), invocation.getParameter(0)); } else if (isMethod(method, void.class, "finalize")) { fakeFinalize(invocation.getInvokedObject()); return null; } else { return next.invoke(invocation); } }
public Object invoke(Invocation invocation) throws Throwable { invocationCount++; performSideEffects(); final Object result = action.invoke(new Invocation(ExpectationMode.ASSERTING, invocation)); invocation.checkReturnTypeCompatibility(result); return result; }
@Override public Object invoke(final Invocation invocation) throws Throwable { final Object injectable = invocation.getInvokedObject(); final Object toInjectInto = invocation.getParameter(0); final Method[] methods = toInjectInto.getClass().getMethods(); for (final Method method : methods) { if (!method.getName().startsWith("set")) { continue; } if (method.getParameterTypes().length != 1) { continue; } final Class<?> methodParameterType = method.getParameterTypes()[0]; if (methodParameterType.isAssignableFrom(injectable.getClass())) { method.invoke(toInjectInto, injectable); break; } } return null; }
public void testMockeryReportsFirstFailedObject() { try { maskedExpectationFailure(mock, otherMock); fail("should have thrown ExpectationError"); } catch (ExpectationError e) { assertSame("invoked object", mock, e.invocation.getInvokedObject()); assertEquals("invoked method", "realExpectationFailure", e.invocation.getInvokedMethod().getName() ); } }
@Override public Object invoke(Invocation invocation) throws Throwable { if(invocation.getParameterCount() != 1) { throw new IllegalArgumentException("intended for action of findByXxx"); } final Object argObj = invocation.getParameter(0); if(!(argObj instanceof String)) { throw new IllegalArgumentException("Argument must be a string"); } String arg = (String) argObj; return getElseCreate(cls, arg); }
public void testMethodToStringWithNullArg() throws Exception { Invocation invocation = new Invocation(INVOKED, methodFactory.newMethod(METHOD_NAME, new Class[]{String.class}, void.class, EXCEPTION_TYPES), new Object[]{null}); String result = invocation.toString(); AssertThat.stringIncludes("Should contain invokedMethod name", METHOD_NAME, result); AssertThat.stringIncludes("Should contain firstArg", "null", result); }
public void testConstructorInterpretsNullParameterValueArrayAsZeroArguments() { Invocation invocation = new Invocation(INVOKED, method); assertEquals("expected no parameters values", 0, invocation.getParameterCount()); }
private Method methodMatching(Invocation invocation) throws NoSuchMethodException { Method invokedMethod = invocation.getInvokedMethod(); return object.getClass().getDeclaredMethod(invokedMethod.getName(), invokedMethod.getParameterTypes()); } }
public void testCanApplyInvocationToAnotherObject() throws Throwable { Target target = new Target(); target.result = "result"; Invocation invocation = new Invocation("receiver", TargetInterface.class.getMethod("doSomething", String.class), new Object[]{"arg"}); String actualResult = (String)invocation.applyTo(target); assertEquals("received argument", "arg", target.receivedArg); assertEquals("result returned from apply", target.result, actualResult); }
public Object applyTo(Object target) throws Throwable { try { return invokedMethod.invoke(target, getParametersAsArray()); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } }
public void testTestsForEqualityOnTargetAndMethodSignatureAndArguments() { Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES); Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES); Invocation differentTarget = new Invocation("OTHER TARGET", method, ARG_VALUES); Invocation differentMethod = new Invocation(INVOKED, methodFactory.newMethod("OTHER_" + METHOD_NAME, ARG_TYPES, RETURN_TYPE, EXCEPTION_TYPES), ARG_VALUES); Invocation differentArgValues = new Invocation(INVOKED, method, new Object[]{new Integer(1), Boolean.FALSE}); assertTrue("should be equal to itself", invocation1.equals(invocation1)); assertTrue("identical calls should be equal", invocation1.equals(invocation2)); assertFalse("should not be equal to object that is not an Invocation", invocation1.equals(new Object())); assertFalse("should not be equal to null", invocation1.equals(null)); assertFalse("should not be equal if different invoked object", invocation1.equals(differentTarget)); assertFalse("should not be equal if different method", invocation1.equals(differentMethod)); assertFalse("should not be equal if different argumentValues", invocation1.equals(differentArgValues)); }
public void testFollowsEqualsHashcodeProtocol() { Invocation invocation1 = new Invocation(INVOKED, method, ARG_VALUES); Invocation invocation2 = new Invocation(INVOKED, method, ARG_VALUES); assertEquals("should have equal hash codes", invocation1.hashCode(), invocation2.hashCode()); }
public Object invoke(Invocation invocation) throws Throwable { final Class<?> returnType = invocation.getInvokedMethod().getReturnType(); if(Object.class.equals(returnType) && invocation.isBuildingExpectation()) { return null; } if (resultValuesByType.containsKey(returnType)) { return resultValuesByType.get(returnType); } if (returnType.isArray()) { return Array.newInstance(returnType.getComponentType(), 0); } if (isCollectionOrMap(returnType)) { final Object instance = collectionOrMapInstanceFor(returnType); if (instance != null) return instance; } if (imposteriser.canImposterise(returnType) && !invocation.isBuildingExpectation()) { return imposteriser.imposterise(this, returnType); } return null; }