2015-04-28 59 views
0

我想使用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”被终止。例如,你可以在游戏中引入一个布尔标志“terminate”。然后,“SapiensSlick”会定期检查该布尔标志,并在其为真时自行终止。然后您的测试用例可以将此标志设置为“true”并等待游戏报告完成。你究竟能够实现这一点取决于你的库房(不知道Slick)。

重点:不要强迫线从外面完成,他必须完成自己!