How do I test a concurrent Java program which expects cmd line arguments?
I have a Java program which its main method (in the main class) expects command line arguments. The program is also concurrent (uses threads and stuff).
I want to do massive refactoring to the program. Before I start refactoring I would like to create a test suit for the main method. I would like to test the main method with different cmd line arguments. I'll want to run these te开发者_StackOverflowsts automatically after each refactoring step I make. How do I create a test which passes cmd line arguments?
I cannot use JUnit because as far as I know it doesn't work well with concurrent programs. I'm also not sure if you can pass cmd line arguments with JUnit.
I'm using eclipse.
Take a look at multithreadedtc. http://code.google.com/p/multithreadedtc/
Consider using JMeter. With JUnit sampler you can make concurrent JUnit tests easily, and see the result. See this question for more details.
I'm not familiar with the various automation tools available specifically for multi-threading, so I won't comment on them. But on simple yet effective option is to log key events from the running program to a CSV file. You could log the final result (if it is a calculation type program) or log out at every instance in the program where some key state is changed or event occurs. Because it's a multi-threaded app you would have to pay attention to comparing the sequence of logged data, if you can not guarantee the relative ordering of the key events you expect to see then compare output using key-value type results. Either way, the idea would be to create test data files which you can use for automated comparison when back testing.
Awaitility can also be useful to help you write deterministic unit tests. It allows you to wait until some state somewhere in your system is updated. For example:
await().untilCall( to(myService).myMethod(), greaterThan(3) );
or
await().atMost(5,SECONDS).until(fieldIn(myObject).ofType(int.class), equalTo(1));
It also has Scala and Groovy support.
await until { something() > 4 } // Scala example
精彩评论