簡體   English   中英

JUnit測試中“方法$未定義”

[英]“Method $ is undefined” in a JUnit test

我的測試代碼使用Junit4-

import static org.junit.Assert.assertEquals;
import junitparams.Parameters;
import org.junit.Test;

public class FootballTeamTest {

    @Test
    @Parameters(method ="nbOfGamesWon")
    public void constructorShouldSetGamesWon(int nbOfGamesWon) {
        FootballTeam team = new FootballTeam(nbOfGamesWon);
        assertEquals(nbOfGamesWon, team.getGamesWon());
    }

    public Object[] nbOfGamesWon(){
        return $(0,1,2);
    }

}

return $(0,1,2);出錯 - The method $(int, int, int) is undefined for the type FootballTeamTest

我的書使用這種代碼沒有問題。 為什么我會發生此錯誤,我該如何解決?

簡單來說,您沒有名為$的方法,該方法具有三個類型為int參數。 定義這樣的方法,您就可以調用它。

public Object[] $(int a, int b, int c) {
    return new Object[0]; // or whatever initialization logic you need
}

請注意,JLS狀態

“ Java字母”包括大寫和小寫的ASCII拉丁字母AZ(\\ u0041- \\ u005a)和az(\\ u0061- \\ u007a),並且由於歷史原因,ASCII下划線(_或\\ u005f)和美元符號($或\\ u0024)。 $符號僅應在機械生成的源代碼中使用,或很少用於訪問舊系統上的現有名稱。

因此,也許您的書中有一些內容。 否則,請勿使用$作為標識符名稱。

這個junitparams庫提供了一個名為$的方法。 您可能會錯過它。

import static junitparams.JUnitParamsRunner.$;

暫無
暫無

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

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