@Before/@BeforeClass po użyciu obiekty to null.

0

Witam.
Od jakiegoś czasu uczę się pisać testy w JUnit.
I niestety mam problem z poprawnym użyciem @Before i @BeforeClass

test poniżej.

package tests;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.jupiter.api.Test;

import Implementation.Croupier;
import Implementation.Player;

public class InitTests {

	private Croupier croupier;
	private Player p1, p2;
	
	@BeforeClass
	public void setUp() {
		croupier = new Croupier();
		croupier.PlayersInit(5, 100);
		Player p1 = croupier.getPlayer(0);
		Player p2 = croupier.getPlayer(1);
	}
	
	
	@Test
	public void PlayerInitTest() {
		assertEquals(0, p1.getId());
		assertEquals(1, p2.getId());
	}
	
	/*@Test
	public void TakeSmallAndBigTests() {
		
		croupier.setActualPlayers();
		croupier.setSmallAndBig();
		croupier.takeFromSmallAndBig();
		
		
		
		assertEquals(80, p1.getCoins());
		assertEquals(90, p2.getCoins());
	}*/

}

klasa Player :

package Implementation;

import java.util.ArrayList;

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();
	}
	
}

klasa Croupier :

package Implementation;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.junit.jupiter.api.Test;

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);
	}
}

Jeżeli inicjacje przeprowadzę na początku to działa bez problemowo, ale niestety jak kombinuję z @Before to już nie działa :(
Niestety nie mam pojęcia co robię źlę. Z góry dziękuję za pomoc.

0

Problem polegał na tym, że korzystałem z mieszanych importów jedne były z JUnit 4 ( import org.junit.Before;
import org.junit.BeforeClass; ) a inne z JUnit 5 (import org.junit.jupiter.api.Test;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1