I have a client server application and for testing purposes I need to start the client in a test method by calling
Client.main();
That creates some new thread. Now I need to wait until that thread is completed before performing assertions in my test. How do I know when this happens? Alternatively, how do I know, which thread was started by the call (the client can create other threads too).
Calling
mainprogrammatically won’t start a new thread on its own. That would only happen if themainmethod has code to explicitly start a new thread. If that’s the case, you should changeClientto provide access to this thread in some form, so you can calljoin()on it. (CallingThread.join()is the standard way of waiting for a thread to finish.)Does
Client.main()perform any tasks other than starting a new thread? If not, it would probably be simpler for your tests to just call whateverrun()method the new thread will end up running, and making the test single-threaded as far as possible.