gpt4 book ai didi

java - @Before/@BeforeClass 似乎不起作用,对象指示为空

转载 作者:搜寻专家 更新时间:2023-11-01 01:31:40 25 4
gpt4 key购买 nike

我正在学习junit,一开始就出现了问题。

一开始我想初始化将在测试中使用的对象。但是@BeforeClass 不会那样做。

public class InitTests {
private Croupier croupier;
private Player p1, p2;
@BeforeClass
public void setUp() {
croupier = new Croupier();
croupier.PlayersInit(5, 100);
p1 = croupier.getPlayer(0);
p2 = croupier.getPlayer(1);
} @Test // p1,p2, croupier = null, have no idea why.
public void PlayerInitTest() {
assertEquals(0, p1.getId());
assertEquals(1, p2.getId());
}}

其他类:

public class Player {

private ArrayList<Card> hand = new ArrayList<>();
private int coins = 0;
private static int playerNumber = 0;
private int id;
private boolean inGame = true;

public Player(int coins) {
this.coins = coins;
id = ++playerNumber;
}

public int addCoins(int amount) {
coins+=amount;
return amount;
}

public int substractCoins(int substract) {
coins-=substract;
return substract;
}

public int getCoins() {
return coins;
}

public int getId() {
return id;
}

public boolean isInGame() {
return inGame;
}

public void setGameStatus(boolean status) {
if(getCoins() < 0 )
inGame = false;
else
inGame = status;
}

public void clearHand() {
hand.clear();
}}

public class Croupier {

private String name;
private ArrayList<Card> deck = new ArrayList<>();
private ArrayList<Player> allPlayers = new ArrayList<>();
private ArrayList<Player> actual = new ArrayList<>();
private int stack = 0;
private int bigPlayerStack = 0;
private int smallPlayerStack = 0;

public Croupier() {
System.out.println("tutej.");
}

public Croupier CroupierInit() {
// static
PlayersInit(5, 100);

return new Croupier();
}

private void CreateDeck() {
String[] suits = { "hearts", "spades", "diamonds", "clubs" };

for (int i = 0; i < suits.length; i++)
for (int j = 2; j < 15; j++)
deck.add(new Card(j, suits[i]));

}

private void DeckShuffle() {
Collections.shuffle(deck);
}

public boolean TurnPlayed() {
if (!preparedGame())
return false;

return true;

}

public void StartGame() {
preparedGame();
System.out.println("Game ended.");
}

public boolean preparedGame() {
clearTable();

if(!setActualPlayers())
return false;

setSmallAndBig();
takeFromSmallAndBig();
CreateDeck();
DeckShuffle();

return true;
}

// set players who are playing
public boolean setActualPlayers() {
for (Player e : allPlayers)
if (e.isInGame())
actual.add(e);

if (actual.size() < 2)
return false;

return true;
}

// take coisn from small and big blind
public void takeFromSmallAndBig() {
stack += actual.get(bigPlayerStack).substractCoins(20);
stack += actual.get(smallPlayerStack).substractCoins(10);
}

// set who has small or big blind
public void setSmallAndBig() {
bigPlayerStack++;
if (bigPlayerStack > actual.size())
bigPlayerStack = 0;

smallPlayerStack = bigPlayerStack - 1;

if(smallPlayerStack < 0 )
smallPlayerStack = actual.size() -1;
}

// clear table before game
public void clearTable() {
actual.clear();
for (Player e : allPlayers)
e.clearHand();
}

public void PlayersInit(int numberOfPlayers, int coins) {
for (int i = 0; i < numberOfPlayers; i++) {
allPlayers.add(new Player(coins));
}
}

public Player getPlayer(int index) {
return allPlayers.get(index);
}}

我确信这些测试是正确的,因为当我将 setUp 方法(来自该方法的代码)放入 @Test 中时,它就可以正常工作。我希望这是一个简单的语法问题,作为初学者目前无法设置。

问候。

最佳答案

您的测试类混合使用了 JUnit 4 和 5 注释。

JUnit 5 API 与以前版本的库不兼容,并定义了一组新的注释。假设您的测试执行,您可能正在使用 JUnit 5 启动器,因此需要使用来自 org.junit.jupiter.api 的注释。包。

如果您想使用 JUnit 5
尝试使用 @org.junit.jupiter.api.BeforeEach@org.junit.jupiter.api.BeforeAll,后者使用静态方法。它们的语义定义为 here .

如果您想使用 JUnit 4
您需要将所有注释恢复为 JUnit 4 - 这意味着使用 org.junit.Test
您的 JUnit 启动器需要针对 JUnit 4 进行配置。

关于java - @Before/@BeforeClass 似乎不起作用,对象指示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com