我开始学习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);
}}
我确定这些测试是正确的,因为当我在@Test中放入setUp方法(来自该方法的代码)时,它才能正常工作。 我希望这是一个简单的语法问题,作为初学者目前无法设置。
问候。
答案 0 :(得分:9)
您的测试类使用JUnit 4和5注释的混合。
JUnit 5 API与库的先前版本不兼容,并定义了一组新的注释。假设您的测试执行,您可能正在使用JUnit 5启动器,因此需要使用org.junit.jupiter.api
包中的注释。
如果您想使用JUnit 5
尝试throw new LocalException($data['message']);
或@org.junit.jupiter.api.BeforeEach
,后者使用静态方法。他们的语义定义为here。
如果您想使用JUnit 4
您需要将所有注释还原为JUnit 4 - 这意味着使用@org.junit.jupiter.api.BeforeAll
您的JUnit启动程序需要为JUnit 4配置。