@Test
public void decrypt_whenThrowsAnError_retriesDecryptionFailure() throws Exception {

  when(keySet.get(activeKeyUuid))
    .thenReturn(firstActiveKey);
  when(keySet.getActive())
    .thenReturn(firstActiveKey);
  when(firstActiveKey.decrypt(any(byte[].class), any(byte[].class)))
    .thenThrow(new ProviderException("function 'C_GenerateRandom' returns 0x30"));
  when(firstActiveKey.getProvider()).thenReturn(encryptionService);
  when(encryptionService.isLoggedIn()).thenReturn(true);
  try {
    subject.decrypt(new EncryptedValue(activeKeyUuid, "an encrypted value".getBytes(UTF_8), "a nonce".getBytes(UTF_8)));
    fail("Expected exception");
  } catch (final ProviderException e) {
    // expected
  }
  final InOrder inOrder = inOrder(firstActiveKey, encryptionService);
  inOrder.verify(firstActiveKey).decrypt(any(byte[].class), any(byte[].class));
  inOrder.verify(encryptionService).reconnect(any(ProviderException.class));
  inOrder.verify(firstActiveKey)
    .decrypt(any(byte[].class), any(byte[].class));
}