2015-09-10 55 views
0

我试图做一个libgdx游戏的JUnit测试,并发现这个线程是非常有帮助的:Unit-testing of libgdx-using classeslibgdx - junit测试 - 如何与应用程序线程通信?

我有类似如下的测试类:

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线程交流,以便我可以完成像描述的测试?

回答

1

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

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 
} 
相关问题