junit 光滑的单元测试-重新运行游戏时出错

xqnpmsa8  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(110)

我想用Junit测试我用slick2d写的游戏(它是建立在LWJGL上的)。(也许做单元测试是个坏主意,但我选择了它作为我的家庭作业。)我在单独的线程中运行游戏,模拟鼠标点击,并做Assert测试。它运行良好,直到我有一个以上的测试功能。(我将“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()到游戏容器,但它再次崩溃。
thread函数在这里:

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)
u4dcyp6a

u4dcyp6a1#

您需要确保“engineTest.run“已终止。例如,你可以在游戏中引入一个布尔标志“终止”。然后,“SapiensSlick”必须周期性地检查这个布尔标志,一旦它为真,就终止自己。然后,您的测试用例可以将此标志设置为“true”,并等待游戏报告它已经完成。如何实现这一点取决于你的库(不知道Slick)。
重要的一点:不要强迫线程完成从外面,他必须完成自己!

相关问题