使用Task类启动线程线程

时间:2012-07-25 14:21:23

标签: concurrency junit task javafx-2

我想测试扩展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
   }
}

但这没有任何作用。在启动的线程中没有执行call方法。有人可以解释为什么会这样吗?

1 个答案:

答案 0 :(得分: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);
    }
}

你会看到,当你运行测试时,它只打印“我累了”但不是“我已经完成了睡眠”(它甚至可能不打印“我累了”,这取决于线程如何交错)。

你可以做的是将你的任务包装在一个runnable中,并与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);
}