繁体   English   中英

不兼容的类型:不存在类型变量 T 的实例,因此 Matcher<t> 符合 ArgumentMatcher<long></long></t>

[英]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);
}

参考: https://www.baeldung.com/mockito-argument-matchers

不兼容的类型:不存在类型变量 F、T 的实例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在尝试将ComplexItem列表转换为它们对应的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)对getCollection()调用进行类型转换后,也不会出现上述错误 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t>

[英]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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不兼容的类型:不存在类型变量 F、T 的实例,因此 java.util.Collection<t> 符合 java.util.Set <java.lang.long< div><div id="text_translate"><p> 我正在尝试将ComplexItem列表转换为它们对应的 ID Long列表。 但是即使在使用(Collection&lt;ComplexItem&gt;)对getCollection()调用进行类型转换后,也不会出现上述错误 go</p><pre> Set&lt;Long&gt; ids = Collections2.transform( getComplexItems(), new Function&lt;ComplexItem, Long&gt;() { @Override public Long apply(final ComplexItem item) { return item.id; } })); public List&lt;ComplexItem&gt; getComplexItems() {.............. }</pre> </div></java.lang.long<></t> Java 错误:不兼容的类型:不存在类型变量 T 的实例,因此可选<t>符合 Iterable<availablelicence></availablelicence></t> 不存在类型变量的实例,因此T符合注释 Java错误:不兼容的类型:不存在类型变量R的实例,因此Stream <R> 符合布尔值 没有类型变量T的实例存在以便List <T> 符合Integer 原因:不存在类型变量 T 的实例,因此 void 符合 T 原因:不存在类型变量 T 的实例,因此 void 符合使用 mockito 不存在类型变量T的实例,因此ID符合Comparable <? super T> 数组排序不起作用,不存在变量 T 的实例,因此 Employee 符合 Comparable Java泛型不兼容的类型(不存在类型变量T的实例)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM