RequestMethod { protected void validateType(final List<Object> parameters, final int index, final Class<?> clazz) { if (parameters.size() <= index) { throw new IllegalArgumentException( String.format("No parameter at index %d (expected: %s)%n", index, clazz)); } final Object parameter = parameters.get(index); if (!clazz.isInstance(parameter)) { throw new IllegalArgumentException(String .format("Parameter at index %d is invalid (expected: %s, actual: %s)%n", index, clazz, parameter.getClass())); } }  String getName(); T invoke(); T invoke(final List<Object> parameters); Invocation<T> toInvocation(); Invocation<T> toInvocation(final List<Object> parameters); @Override String toString(); @Override boolean equals(final Object obj); @Override int hashCode();  }
@Test public void testValidateType() throws Exception { final List<Object> parameters = Arrays.<Object>asList("string", 1, new LinkedList<>()); final List<Class<?>> types = Arrays.<Class<?>>asList(String.class, Integer.class, LinkedList.class); final RequestMethod<?> requestMethod = spy(RequestMethod.class); for (int i = 0; i < parameters.size(); ++i) { requestMethod.validateType(parameters, i, types.get(i)); } }
@Test public void shouldValidateTypeThrowExceptionOnInvalidType() throws Exception { final List<Object> parameters = Arrays.<Object>asList("string", 1, new LinkedList<>()); final RequestMethod<?> requestMethod = spy(RequestMethod.class); try { requestMethod.validateType(parameters, 4, String.class); fail("Should throw IllegalArgumentException on illegal index"); } catch (IllegalArgumentException e) { } try { requestMethod.validateType(parameters, 0, Integer.class); fail("Should throw IllegalArgumentException on illegal type"); } catch (IllegalArgumentException e) { } }