### Question:
LockTemplate { public <T> T withReadLock(LockedOperation<T> operation) { Lock lock = this.monitor.readLock(); lock.lock(); try { return operation.invoke(); } catch (Exception e) { this.logger.error("Error while invoking read-locked operation", e); throw new RuntimeException(e); } finally { lock.unlock(); } } LockTemplate();   LockTemplate(ReadWriteLock monitor); T withReadLock(LockedOperation<T> operation); T withWriteLock(LockedOperation<T> operation);  }### Answer:
@Test public void withReadLock() throws Exception { when(this.operation.invoke()).thenReturn("test-value"); String actual = this.lockTemplate.withReadLock(this.operation); assertEquals("test-value", actual); verify(this.readLock).lock(); verify(this.readLock).unlock(); }
@Test public void withReadLockException() throws Exception { when(this.operation.invoke()).thenThrow(new Exception()); try { this.lockTemplate.withReadLock(this.operation); fail(); } catch (RuntimeException e) { assertTrue(e.getCause() instanceof Exception); } verify(this.readLock).lock(); verify(this.readLock).unlock(); }