@Test
public void encrypt_whenThrowsAnError_unlocksAfterExceptionAndLocksAgainBeforeEncrypting() throws Exception {

  when(keySet.getActive())
    .thenReturn(firstActiveKey);
  when(firstActiveKey.encrypt("a value"))
    .thenThrow(new ProviderException("function 'C_GenerateRandom' returns 0x30"));
  reset(writeLock);
  try {
    subject.encrypt("a value");
  } catch (final ProviderException e) {
    // expected
  }
  verify(readLock, times(2)).lock();
  verify(readLock, times(2)).unlock();
  verify(writeLock, times(1)).unlock();
  verify(writeLock, times(1)).lock();
}