Skip to content

Instantly share code, notes, and snippets.

@samshu
Created May 29, 2017 11:26
Show Gist options
  • Save samshu/8b750c8a2197860cbb7f84a6fad9696b to your computer and use it in GitHub Desktop.
Save samshu/8b750c8a2197860cbb7f84a6fad9696b to your computer and use it in GitHub Desktop.
A Mockito ArgumentMatcher for java 8 Optional of array objects.
@SuppressWarnings("rawtypes")
public class OptionalArrayArgumentMatcher extends ArgumentMatcher<Optional> {
private final Optional compareWith;
public OptionalArrayArgumentMatcher(Optional optionalArray) {
this.compareWith = optionalArray;
}
@Override
public boolean matches(Object argument) {
Optional arg = (Optional) argument;
if (!compareWith.isPresent() && !arg.isPresent()) {
return true;
}
if (compareWith.isPresent() != arg.isPresent()) {
return false;
}
Object[] argArray = (Object[]) arg.get();
Object[] compareWithArray = (Object[]) compareWith.get();
return Arrays.equals(argArray, compareWithArray);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment