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);
}