【发布时间】:2020-12-22 13:55:39
【问题描述】:
我正在用 Java FXGL 编写一个简单的游戏。我对 Java FX 和 FXGL 非常陌生。
我想用 JUnit5 测试我的游戏,但我无法让它工作...... 问题:当我启动测试时,FXGL 属性尚未初始化。
如果有人能给我一个想法,我会很高兴,如何使用FXGL.getWorldProperties()为班级女巫发起测试
我的班级:
public class Player {
private int playerColumn = 2; // The actual column from the player
private int nColumns; // Numbers of Columns in the game // Will be set on the init of the game
public int getPlayerColumn() {
return playerColumn;
}
// Some more code ...
/**
* The Input Handler for moving the Player to the right
*/
public void moveRight() {
if (playerColumn < nColumns - 1) {
playerColumn++;
}
updatePlayerPosition();
}
/**
* Calculates the x Offset for the Player from the column
*/
private void updatePlayerPosition() {
getWorldProperties().setValue("playerX", calcXOffset(playerColumn, playerFactory.getWidth()));
}
// Some more code ...
}
我的测试班:我不知道我该怎么做...
@ExtendWith(RunWithFX.class)
public class PlayerTest{
private final GameArea gameArea = new GameArea(800, 900, 560, 90);
private final int nColumns = 4; // Numbers of Columns in the game
@BeforeAll
public static void init(){
Main.main(null);
}
@Test
public void playerColumn_init(){
Player player = new Player();
assertEquals(2, player.getPlayerColumn());
}
@Test
public void moveLeft_2to1(){
Player player = new Player();
player.moveLeft();
assertEquals(1, player.getPlayerColumn());
}
}
在这种情况下,测试根本不会启动,因为程序被困在游戏循环中......
但是,如果我让Main.main(null); - 调用,则探针不会初始化
【问题讨论】: