tempus-fugit

Java micro-library for writing & testing concurrent code

Make JMock Thread Safe

By default, JMock’s “context” is not thread safe. All bets are off if you access the Mockery from multiple threads. Happily, since JMock 2.6.0, you can set a threading policy per mockery.

1
2
3
Mockery mockery = new JUnit4Mockery() {{
  setThreadingPolicy(new Synchroniser());
}};

The Synchroniser forces serialisation of each mocked method call using synchronized. Use it when you’re running multi-threaded style tests using JMock. The default behaviour will warn you if a mockery is being used like this.

the Mockery is not thread-safe: use a Synchroniser to ensure thread safety

Over to you...