我正在用javafxgl编写一个简单的游戏。我对javafx和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);
-打电话过去,问题还没有解决
1条答案
按热度按时间wnvonmuf1#
通常,您有两种选择:
这是推荐的方法,因为我假设您希望对自己的代码进行单元测试。
FXGL.getWorldProperties()
返回一个PropertyMap
. 而不是依赖于FXGL.getWorldProperties()
,你可以Player
类依赖于PropertyMap
相反。例如:不建议这样做,尽管它可以作为集成测试使用。启动整个游戏
GameApplication.launch(YourApp.class)
在另一个线程中(它将阻止直到游戏结束)。然后正常测试你的游戏@Test
.