Created
May 29, 2017 11:26
-
-
Save samshu/8b750c8a2197860cbb7f84a6fad9696b to your computer and use it in GitHub Desktop.
A Mockito ArgumentMatcher for java 8 Optional of array objects.
This file contains 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
@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