轉自:https://segmentfault.com/a/1190000003762719
其實junit是將test作為參數傳遞給了TestRunner的main函數。並通過main函數進行執行。
test函數在main中執行。如果test執行結束,那么main將會調用System.exit(0);即使還有其他的線程在運行,main也會調用System.exit(0);
System.exit()是系統調用,通知系統立即結束jvm的運行,即使jvm中有線程在運行,jvm也會停止的。所以會出現之前的那種情況。其中System.exit(0);的參數如果是0,表示系統正常退出,如果是非0,表示系統異常退出。
junit.textui.TestRunner
public static void main (String[] args) {
TestRunner aTestRunner = new TestRunner();
try {
TestResult r = aTestRunner.start(args);
if (!(r.wasSuccessful()))
System.exit (1);
System.exit(0);
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(2);
}
}
我用GroboTestingJUnit-1.2.1-core導入工程就能用JUnit進行多線程測試了
下載地址:
https://download.csdn.net/download/wangqingqi20005/9636371
——————————————————2018-4-15日更新————————————————————————
最好還是用main進行多線程測試,就算導入了GroboTestingJUnit-1.2.1-core.jar包還是會有bug
首先是代碼部分:
thread2

運行部分代碼:

test10運行結果:

main運行結果:

結果一目了然,不相信的同學可以試一下,我是推薦用main進行測試
