@Test
public void encrypt_whenThrowsAnError_retriesEncryptionFailure() throws Exception {

  when(keySet.getActive())
    .thenReturn(firstActiveKey);
  when(firstActiveKey.encrypt("a value"))
    .thenThrow(new ProviderException("function 'C_GenerateRandom' returns 0x30"));
  when(firstActiveKey.getProvider()).thenReturn(encryptionService);
  when(encryptionService.isLoggedIn()).thenReturn(true);
  try {
    subject.encrypt("a value");
    fail("Expected exception");
  } catch (final ProviderException e) {
    // expected
  }
  final InOrder inOrder = inOrder(firstActiveKey, encryptionService);
  inOrder.verify(firstActiveKey).encrypt(anyString());
  inOrder.verify(encryptionService).reconnect(any(ProviderException.class));
  inOrder.verify(firstActiveKey).encrypt(anyString());
}