Created
September 20, 2011 19:51
-
-
Save jamesmorgan/1230140 to your computer and use it in GitHub Desktop.
Simple List Object Array Matcher with usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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