Skip to content

Instantly share code, notes, and snippets.

@jamesmorgan
Created September 20, 2011 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmorgan/1230140 to your computer and use it in GitHub Desktop.
Save jamesmorgan/1230140 to your computer and use it in GitHub Desktop.
Simple List Object Array Matcher with usage
/**
* An expected call to my matcher
*/
this.context.checking(new Expectations() {
{
one(someMockedService).save(with(args), with(def), with(thisObjectArray(batch)));
}
});
this.seviceUnderTest.source(args);
/**
* Static method for use in my unit test
*/
public static Matcher<List<Object[]>> thisObjectArray(final List<Object[]> batch) {
return new ObjectArrayMatcher(batch);
}
/**
* Object Array Matcher
*/
private static class ObjectArrayMatcher extends BaseMatcher<List<Object[]>> {
private final List<Object[]> batch;
public ObjectArrayMatcher(final List<Object[]> batch) {
this.batch = batch;
}
@Override
@SuppressWarnings("unchecked")
public boolean matches(final Object arg) {
if (!(arg instanceof List)) {
return false;
}
final List<Object[]> converted = (List<Object[]>) arg;
if (converted.size() != batch.size()) {
return false;
}
for (int i = 0; i < converted.size(); i++) {
assertThat(converted.get(i), is(batch.get(i)));
}
return true;
}
@Override
public void describeTo(final Description arg) {
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment