gpt4 book ai didi

java - libgdx - junit 测试 - 如何与应用程序线程通信?

转载 作者:行者123 更新时间:2023-12-01 11:09:57 25 4
gpt4 key购买 nike

我正在尝试对 libgdx 游戏进行 junit 测试,并且发现此线程非常有帮助:Unit-testing of libgdx-using classes

我有一个类似于以下内容的测试类:

public class BoardTest {

private static Chess game;
private static HeadlessApplication app;

@BeforeClass
public static void testStartGame() {

game = new Chess();

final HeadlessApplicationConfiguration config = new HeadlessApplicationConfiguration();
config.renderInterval = 1f/60; // Likely want 1f/60 for 60 fps
app = new HeadlessApplication(game, config);

}

@Test
public void testSetUpBoard() {

final boolean isFalse = false;

Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {

//do stuff to game
fail(); //see if the test will fail or not

}
});
}
}

当我运行这个测试类时,它运行 testSetUpBoard() 并通过,而不是像它应该的那样失败。我认为,这是因为执行的代码按照 Gdx.app.postRunnable() 位于单独的线程中。有什么方法可以与 junit 线程通信,以便我可以像所描述的那样完成测试?

最佳答案

您可以像这样等待线程完成:

private boolean waitForThread = true;

@Test
public void testSetUpBoard() {


final boolean isFalse = false;

Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
//do stuff to game
waitForThread = false;
}
});

while(waitForThread) {
try {
Thread.sleep(10);
} catch(Exception e ) {
}
}

// fail or pass...
fail(); //see if the test will fail or not
}

关于java - libgdx - junit 测试 - 如何与应用程序线程通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32492526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com