JMock and Threads: Error Diagnostics

Another difficulty testing multithreaded code is that errors occur on the threads under test and are reported by exceptions thrown on those threads. The test thread cannot easily trap those exceptions and fail the test with a helpful diagnostic message. Often tests fail with a timeout, which has no message explaining why the timeout was exceeded. Or, an assertion on the test thread fails because of some error that occurred on a background thread, but information about the root cause is lost.

JMock addresses these problems by capturing the first expectation failure that is thrown from a mock object. If the error is swallowed, either by a background thread or by a try/catch block in the foreground thread, the test will continue running and the mockery will throw the error when the test asserts that all expected calls have been made. This is usually done automatically at the end of the test.

The Synchroniser's wait methods also fail fast and rethrow expectation violations that occur on background threads. A test can wait for the system to enter an expected state but if it does not do so because the system violated an expectation, the wait will immediately abort and throw the error that originally reported the violated expectation.

The Mockery records a trace of all the invocations that any mock object has received during the test and includes the trace recorded so far when it throws an expectation error.

The combination of failing fast, reporting the root cause of failure, and reporting the sequence of calls that led to the failure makes diagnosis much easier than a bare "test timed out" message.

Copyright © 2009 Nat Pryce. Posted 2009-03-19. Share it.

Comments powered by Disqus