1

我想测试扩展 javafx.concurrent.Task 的任务类。我已经覆盖了调用方法:

   public class myTask extends Task<Void> {
     @Override
     protected Void call() throws Exception {
       while(!isCancelled()){
         doSth();
       }
       return null;
     }
   }

然后我想用 JUnit 测试来测试该方法的调用:

public class MyTaskTest {
   @Test
   public void testCall() throws Exception {
     MyTask task = new MyTask();
     Thread th = new Thread(task);
     th.start();
     //.... further validation
   }
}

但这无济于事。在启动的线程中没有执行调用方法。有人可以解释为什么会这样吗?

4

1 回答 1

1

JUnit 测试不会等待您的任务线程执行它需要执行的操作,并且会在 JUnit 线程完成后立即终止。您可以通过一个简单的示例查看该行为:

测试类:

public class Test1 implements Runnable {
    @Override
    public void run() {
        System.out.println("I'm tired");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
        }
        System.out.println("I'm done sleeping");
    }

}

测试类:

public class Test1Test {
    @Test
    public void testRun() {
        Test1 task = new Test1();
        Thread th = new Thread(task);
        th.start();
        boolean yourTestedStuff = true;
        assertTrue(yourTestedStuff);
    }
}

您会看到,当您运行测试时,它只打印“我累了”而不是“我睡完了”(它甚至可能不打印“我累了”,具体取决于线程的交错方式)。

您可以做的是将您的任务包装在一个可运行的文件中,并与 jUnit 线程进行某种形式的同步,例如通过 CountDownLatch,例如:

@Test
public void testRun() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final Test1 task = new Test1();
    Runnable r = new Runnable() { //wrap your task in a runnable

        @Override
        public void run() {
            task.run();  //the wrapper calls you task
            latch.countDown();  //and lets the junit thread when it is done
        }
    };
    Thread th = new Thread(r);
    th.start();
    assertTrue(latch.await(1000, TimeUnit.SECONDS)); //force junit to wait until you are done
    boolean yourTestedStuff = true;
    assertTrue(yourTestedStuff);
}
于 2012-07-25T16:42:11.447 回答