我想用Junit测试来测试我在slick2d(基于LWJGL构建)上编写的游戏。(或许做单元测试是个坏主意,但我选择它作为我的作业。)我运行游戏在单独的线程中并模拟鼠标单击并执行断言测试。它一直顺利,直到我有多个测试功能。 (我将" test"函数复制并粘贴到第二个函数)创建另一个gameContainer并在第二个测试函数中实际游戏导致我的测试崩溃。我想我需要在测试后以某种方式重置游戏上下文,但我不知道如何。
这是我的测试:(我尽可能地简化了它)
@Test
public void test() throws InterruptedException {
Thread t = new Thread(new engineTest());
t.start();
Thread.sleep(3000); // wait for game to initialize
// Game simulation and asserts are here
t.interrupt();
Thread.sleep(1000);
}
我尝试了很多变种,包括将.exit()和.destroy()调用到游戏容器中,但它再次崩溃。
线程函数在这里:
public class engineTest implements Runnable {
@Override
public void run() {
SapiensSlick.main(null); // SapiensSlick is name of the game
}
}
当第二个功能正在进行时,我收到此错误:
[xcb] Unknown request in queue while dequeuing
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
java: ../../src/xcb_io.c:179: dequeue_pending_request: Předpoklad „!xcb_xlib_unknown_req_in_deq“ nesplněn.
hello
Tue Apr 28 10:50:07 CEST 2015 INFO:Slick Build #237
Testsuite: JunitTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
Testcase: JunitTest:test: Caused an ERROR
Forked Java VM exited abnormally. Please note the time in the report does not reflect the time until the VM exit.
junit.framework.AssertionFailedError: Forked Java VM exited abnormally. Please note the time in the report does not reflect the time until the VM exit.
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)
答案 0 :(得分:0)
您需要确保“engineTest.run”已终止。例如,你可以在游戏中引入一个布尔标志“终止”。然后,“SapiensSlick”会定期检查此布尔标志,并在它为真时终止自身。然后,您的测试用例可以将此标志设置为“true”,并等待游戏报告它已完成。你如何实现这一点取决于你的图书馆(不知道Slick)。
导入点:不要强迫线程从外面完成,他必须完成自己!