Java Library for Testing Asynchronous Systems
Here is a common integration testing anti-pattern:
sendRequest(); Thread.sleep(5000); checkThatResponseIsCorrect();
If the response is returned quickly then the test will still wait for 5 seconds before moving on, making your test suite slow. On the other hand, if the response is returned correctly but takes slightly longer than 5 seconds then the test will fail, which may not be what you want.
Using the asynctest library, you could re-write the test like this:
Behind the scenes the library will run the
checkThatResponseIsCorrect method repeatedly, stopping when the method completes without throwing any Exceptions, or when the time limit of 15 seconds is reached.
To specify a different time limit, you can do this:
To assert that an Exception does get thrown, just change the code to this:
When an assertion fails you will see a comprehensive error message explaining what happened, for example:
java.lang.AssertionError: Operation did not throw org.apache.commons.httpclient.HttpException within the timeout interval of 5 seconds. The task ran 5 times. There were no AssertionErrors. No other Exceptions were thrown.