Last active
August 29, 2015 14:07
-
-
Save 4ndrew/e40db01a893a3fa20943 to your computer and use it in GitHub Desktop.
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
@FunctionalInterface | |
public static interface GenericArgumentMatcher<T>{ | |
boolean matches(T argument); | |
} |
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
public static class GenericMatchers { | |
public static class GenericArgumentMatcherWrapper<T> extends ArgumentMatcher<T> { | |
private final GenericArgumentMatcher<T> delegate; | |
public GenericArgumentMatcherWrapper(GenericArgumentMatcher<T> delegate) { | |
this.delegate = delegate; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public boolean matches(Object argument) { | |
try { | |
return delegate.matches((T) argument); | |
} catch (ClassCastException e) { | |
return false; | |
} | |
} | |
} | |
public static <T> T argThat(GenericArgumentMatcher<T> matcher) { | |
return Matchers.argThat(new GenericArgumentMatcherWrapper<T>(matcher)); | |
} | |
} |
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
public class SampleJava8Test { | |
public void testTest() { | |
final MyClass m = mock(MyClass.class); | |
when(m.getSomething(GenericMatchers.argThat(a -> a.isValid()))).thenReturn(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment