So I have a method that starts five threads. I want to write a unit test just to check that the five threads have been started. How do I do that? Sample codes are much appreciated.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Instead of writing your own method to start threads, why not use an
Executor, which can be injected into your class? Then you can easily test it by passing in a dummyExecutor.Edit: Here’s a simple example of how your code could be structured:
And here’s how you’d call it from your
main():And here’s how you’d test it (assuming JUnit 4 and Mockito):
Note that all this code is just a sketch which I have not tested or even tried to compile yet. But it should give you an idea of how the code can be structured.