Search code examples
javamultithreadingjunitthread-safetyassert

Is Java org.junit.Assert thread safe?


I want to verify if assert, and assertEquals is thread safe or not. I am under the understanding that it is not, as I believe I am running into race conditions wile using it for my JUnit tests. There are other ways I can go about verifying results of course. Its just simply out of convenience that I am using this function. I have looked at http://junit.sourceforge.net/javadoc/org/junit/Assert.html for reference, and there's no mention of any internal synchronization, or locking. Thanks.


Solution

  • For any reasonable implementation, and the one I randomly googled, assertTrue and assertEquals are thread-agnostic themselves. Thread agnosticism is the usual state of the world.

    For the values are changing then, for the Object overload, equals on the expected object will need to be thread-safe and necessarily the values from toString may be different.

    The fail message is passed via a String to the simple AssertionFailedError, which all looks in order, although Throwable is not safe from unsafe publication (not much mutable is).