Skip to content

Instantly share code, notes, and snippets.

@RobertFischer
Created May 23, 2012 06:04
Show Gist options
  • Save RobertFischer/2773494 to your computer and use it in GitHub Desktop.
Save RobertFischer/2773494 to your computer and use it in GitHub Desktop.
An assert to affirm a method exists
// The signature that works out more naturally
void assertHasMethod(Object obj, String name, Class[] types) {
def methods = obj.metaClass.respondsTo(obj, name, types)
if(methods.isEmpty()) fail("Could not find method $name($types) on $obj")
}
// Enables [Foo,Bar,Baz] notation
void assertHasMethod(Object obj, String name, List<Class> types) {
assertHasMethod(obj, name, types.toArray(new Class[0]))
}
@RobertFischer
Copy link
Author

This will check for both static and dynamic types. However, the dynamic types will need to be injected/configured/whatever for your test cases to detect them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment