Java – @ before / @ beforeclass doesn’t seem to work, and the object represents null
I was learning JUnit and had a problem at the beginning
At the beginning, I want to initialize the object that will be used in the test But @ beforeclass does not
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());
}}
Other courses:
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);
}}
I'm sure these tests are correct because it works when I put the setup method (code from that method) in @ test I hope this is a simple grammar problem, which can't be set as a beginner at present
to greet.
Solution
Your test class uses a mix of JUnit 4 and 5 annotations
The JUnit 5 API is incompatible with previous versions of the library and defines a new set of annotations Assuming that your test is running, you may be using the JUnit 5 launcher, so you need to use org junit. jupiter. Comments in the API package
If you want to use JUnit 5, try @ org junit. jupiter. api. BeforeEach or@org.junit.jupiter.api.BeforeAll , use the static method of the latter Their semantic definition is here
If you want to use JUnit 4, you need to restore all comments to JUnit 4 - which means using org. Org junit. Test. Your JUnit launcher needs to be configured for JUnit 4
