簡體   English   中英

對於沒有通配符類型的方法,為什么我在 Mockito 3 中得到“method thenReturn undefined”?

[英]Why am I getting "method thenReturn undefined" with Mockito 3 for methods without wildcard types?

以下代碼給出了編譯器錯誤: The method thenReturn(null) is undefined for the type FrequencySummary在星號行中The method thenReturn(null) is undefined for the type FrequencySummary

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import edu.mills.cs180a.wordnik.client.api.WordApi;

public class SimpleTest {
    private WordApi mockWordApi = mock(WordApi.class);

    @BeforeEach
    void setup() {
        when(mockWordApi.getWordFrequency(anyString(), "true", anyInt(), anyInt())
            .thenReturn(null); // *** COMPILER ERROR ON THIS LINE
        when(mockWordApi.getWordFrequency("apple", "false", 2000, 2001))
            .thenReturn(null);
    }
}

我知道Mockito 對帶有通配符的方法給出了這個錯誤 然而, WordApi.getWordFrequency的簽名是:

FrequencySummary getWordFrequency(@Param(value="word") String word, @Param(value="useCanonical") String useCanonical, @Param(value="startYear") Integer startYear, @Param(value="endYear") Integer endYear)

該方法的另一個版本具有參數化類型,但具有不同的參數計數且沒有通配符:

FrequencySummary getWordFrequency(@Param(value="word") String word, @QueryMap(encoded=true) Map<String, Object> queryParams)

如您所見,方法聲明中有注釋。 這會破壞thenReturn()嗎?

WordApi類是由 Spring 和 Swagger 為 Feign 生成的。 我在 Mockito 3.6.0 中使用 Java 15。

在加星標的行中,您缺少一個右括號)之后

anyInt())

結果thenReturn叫你的嘲笑WordApi ,而不是嘲諷的Mockito API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM