簡體   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