1. 程式人生 > >Junit| 採用Junit測試時,注意使用者的執行緒會自動停止

Junit| 採用Junit測試時,注意使用者的執行緒會自動停止

採用Junit測試時,注意使用者的執行緒會自動停止什麼意思呢?

正常的程式執行時,JVM的停止是在所有使用者執行緒(也就是非守護執行緒)執行完畢後才推出JVM,但是如果是在JUnit測試的@Test方法中測試,程式正常執行完畢後用戶執行緒會自動推出而不是一直的得帶所有使用者執行緒執行完畢

public class TestJunit {

    public static void main(String[] args) {
        new TestJunit().testThread();
        System.out.println("main thread is destroy."
); } public void testThread() { Thread t = new Thread(new Runnable() { @Override public void run() { for (;;) { } } }); t.start(); } }

執行的輸出結果:
main thread is destroy.然後程式一直處於等待狀態.

在Junit測試中

public
class TestJunitTest { @Test public void test() { new TestJunit().testThread(); System.out.println("test finish"); } }

輸出結果:
test finish然後程式立馬終止.