![](/img/trans.png)
[英]incompatible types: no instance(s) of type variable(s) F,T exist so that java.util.Collection<T> conforms to java.util.Set<java.lang.Long
[英]incompatible types: no instance(s) of type variable(s) T exist so that Matcher<T> conforms to ArgumentMatcher<Long>
private long userId = 1;
@Before
public void before() {
when(userStatisticsDAO.findByUserIDAndCurrency(longThat(isLessThanOrEqualTo(userId)), any(Currency.class))).thenReturn(userStatistics);
}
public static <T extends Comparable<T>> Matcher<T> isLessThanOrEqualTo(T value) {
return OrderingComparison.lessThanOrEqualTo(value);
}
错误发生在:
longThat(isLessThanOrEqualTo(userId)
incompatible types: no instance(s) of type variable(s) T exist so that Matcher<T> conforms to ArgumentMatcher<Long>
您可以创建一个ArgumentMatcher
实现以与longThat(...)
一起使用。
您创建的isLessThanOrEqualTo
方法不是longThat(...)
方法所期望的ArgumentMatcher
类型。
请参阅下面的示例
@Test
public void test() {
long userId = 10;
when(userStatisticsDAO.findByUserIDAndCurrency(
ArgumentMatchers.longThat(new LessThanEquals(userId)), any(Currency.class)))
.thenReturn(userStatistics);
Assert.assertNull(userStatisticsDAO.findByUserIDAndCurrency(199, currency));
Assert.assertNotNull(userStatisticsDAO.findByUserIDAndCurrency(1, currency));
Assert.assertNotNull(userStatisticsDAO.findByUserIDAndCurrency(10, currency));
}
class LessThanEquals implements ArgumentMatcher<Long> {
final long limit;
public LessThanEquals(long limit) {
this.limit = limit;
}
public boolean matches(Long input) {
return input != null && input <= limit;
}
}
// Don't use this, since it's not returning an ArgumentMatcher
public static <T extends Comparable<T>> Matcher<T> isLessThanOrEqualTo(T value) {
return OrderingComparison.lessThanOrEqualTo(value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.