Open GoogleCodeExporter opened 8 years ago
Yeah, this seems to be a nice idea
Original comment by gabor.li...@gmail.com
on 19 Nov 2012 at 5:29
Just copied the pastebin content to have it right in the issue
import org.mockito.ArgumentMatcher;
/**
* Abstract implementation of {@link ArgumentMatcher} to get rid of the {@link Object} in the method-signature.
*
* @author florian.patzl
*
*/
public abstract class GenericArgumentMatcher<T> extends ArgumentMatcher<T> {
@SuppressWarnings("unchecked")
@Override
public boolean matches(Object argument) {
try {
return doesMatch((T) argument);
} catch (ClassCastException e) {
return false;
}
}
protected abstract boolean doesMatch(T argument);
}
Could be interesting.
Original comment by brice.du...@gmail.com
on 27 Nov 2012 at 5:00
I don't understand why ArgumentMatcher<T> doesn't do this already? The class
is generic so why does the matches() method take Object and not T?
Original comment by DanKing...@googlemail.com
on 22 Oct 2013 at 10:37
Java8 variant: https://gist.github.com/4ndrew/e40db01a893a3fa20943
@FunctionalInterface
public static interface GenericArgumentMatcher<T>{
boolean matches(T argument);
}
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));
}
}
public class SampleTest {
public void testTest() {
final MyClass m = mock(MyClass.class);
when(m.getSomething(GenericMatchers.argThat(a -> a.isValid()))).thenReturn(true);
}
}
Original comment by Andrew.P...@gmail.com
on 7 Oct 2014 at 5:00
Original issue reported on code.google.com by
florian....@gmail.com
on 19 Nov 2012 at 4:57