Skip to content
Advertisement

Are Java Mockito mocked method invocations thread safe?

I am writing unit tests that test the thread safety of individual Java classes. I use Mocktio to set up the tests and verify the interactions in a multithreaded environment are done as per the expectations, and threads do not interfere with business expectations.

Are Mockito mocks thread-safe in that context? Can the mocked methods be called by many threads and the invocations will be counted correctly?

Advertisement

Answer

Yes, they are. Quoting mockito documentation.

(…) you can let multiple threads call methods on a shared mock to test in concurrent conditions.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement