@Test
public void storeSecrets_encryptionError() {

    //GIVEN
    final TypedSecret typedSecret = new TypedSecret();
    typedSecret.setSecretType(SecretType.E_METER_MASTER_KEY);
    typedSecret.setSecret(HexUtils.toHexString("$3cr3t".getBytes()));
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(EncryptionProviderType.JRE);
    keyReference.setReference("keyReferenceString");
    //WHEN
    when(this.secretRepository.findIdOfValidMostRecent(any(), any(), any())).thenReturn(null);
    when(this.keyRepository.findByTypeAndValid(any(), any(), any())).thenReturn(
            new PageImpl<>(Arrays.asList(keyReference)));
    when(this.encryptionDelegate.encrypt(any(), any(), anyString())).thenThrow(
            new RuntimeException("Encryption error"));
    //THEN
    assertThatIllegalStateException().isThrownBy(
            () -> this.service.storeSecrets("SOME_DEVICE", Arrays.asList(typedSecret)));
}