How to make JUnit behave the same as the Java running from command line
I noticed that when a unit test exits, all the threads spawned are automatically killed, this is quite different from th开发者_开发技巧e java program running from command line. In the command line, the program does not exit until all the non deamon threads exit.
How to test the case involving spawned thread using jUnit?
If the method you are testing returns a reference to the thread it creates, then join on that. If it doesn't, create your own thread to call the method under test and join on that.
I would suggest you make your unit test shutdown() its components such that it returns to the state before the test started. This may not be actually required in the running system but it allows you to run multiple tests many times.
You may even like to take a snapshot of the running threads at the start and check after the test that there are no new threads (there may be some expected ones which are okay)
精彩评论