The problem of multithreading in Junit unit testing is that the unit testing in junit is multithreading.
This afternoon, I quickly completed an interface monitoring function, and then started the unit test with Junit. Then I began to fall into the verge of collapse...
After the monitoring is completed, the monitoring results need to be sent to the operations staff for maintenance by email. The previous test is still very smooth, and it will not work when sending multi-thread mail,
The program does not report errors or receive emails. Then, change the code and test again. Then, try again, and then test again. The result is fruitless. Finally, choose du Niang. The conclusion is:
The Junit unit test does not support multithreading.
Then, The whole person is not good... a waste of time, it is because of this !!!
Although I know the result, I still need to verify it myself.
/*** @ Title: TestDoWork. java * @ Describe: * @ author: Mr. yanphet * @ Email: mr_yanphet@163.com * @ date: August 15, 2016 5:50:03 * @ version: 1.0 */public class TestDoWork {class DoWork implements Runnable {@ Override public void run () {for (int I = 0; I <10000; I ++) {long milliSecond = System. currentTimeMillis (); System. out. println ("I =" + I + ", milliSecond =" + milliSecond); // output cycle times and current system time }}@ Test public void test () {DoWork dw = new DoWork (); Thread t = new Thread (dw); t. start ();}}
The output result is as follows (some output is omitted ):
.....i=751,milliSecond=1471257586416i=752,milliSecond=1471257586416i=753,milliSecond=1471257586416i=754,milliSecond=1471257586416i=755,milliSecond=1471257586416i=756,milliSecond=1471257586416i=757,milliSecond=1471257586416i=758,milliSecond=1471257586416
From the results, we can see that the task is not output after the loop reaches 759, indicating that the entire program is forced to end before the sub-thread finishes the task.
Now that we know the phenomenon, why does it happen? We can find out some source code of Junit4 TestRunner.
public static final int SUCCESS_EXIT = 0;public static final int FAILURE_EXIT = 1;public static final int EXCEPTION_EXIT = 2;public static void main(String args[]) { TestRunner aTestRunner = new TestRunner(); try { TestResult r = aTestRunner.start(args); if (!r.wasSuccessful()) System.exit(FAILURE_EXIT); System.exit(SUCCESS_EXIT); } catch (Exception e) { System.err.println(e.getMessage()); System.exit(EXCEPTION_EXIT); }
}
Paste the TestResult source code for your reference.
protected List<TestFailure> fFailuresprotected List<TestFailure> fErrorspublic synchronized boolean wasSuccessful() { return failureCount() == 0 && errorCount() == 0;}public synchronized int errorCount() { return fErrors.size();}public synchronized int failureCount() { return fFailures.size();}
It can be seen from the TestRunner that, if it is a single thread, when the test main thread finishes executing, whether the non-pipe thread ends or not, it will call back the wasSuccessful method of TestResult,
Then determine whether the result is successful or failed, and finally call the corresponding System. exit () method. We all know that this method is used to end the currently running Java virtual machine.
It's hard to protect yourself, so the sub-thread can't help you...
Solution:
1. Let the main thread sleep for a period of time, and then let the sub-thread end. But the disadvantage of this method is that you do not know the running time of the sub-thread, so you need to look at the face = _ =
Thread. sleep ();
2. Use the CountDownLatch tool class to block the main thread until the sub-thread stops running or the blocking times out. This method is better than the first method.
CountDownLatch. await (5, TimeUnit. MINUTES );
As for other methods, I can see that many of the Junit's self-written by the great gods support multithreading. Interested readers are independent...