@Test
void testRetryWhenErrorHappensWhilePersisting() {

    final DocumentArchivingContext documentArchivingContext = createDocumentArchivingContext();
    final DocumentCreationContext documentCreationContext = DocumentCreationContext.builder()
            .build();
    when(documentCreationContextFactory.newContext(documentArchivingContext))
            .thenReturn(Mono.just(documentCreationContext));
    final DocumentEntity documentEntity = DocumentEntity.builder()
            .build();
    when(documentEntityFactory.newDocumentEntity(documentCreationContext))
            .thenReturn(Mono.just(documentEntity));
    final MongoWriteException mongoWriteException = mock(MongoWriteException.class);
    when(mongoWriteException.getMessage()).thenReturn("E11000 duplicate key error");
    //Do a normal exception, then a retry happens and throw the mongo exception to stop the retries
    doThrow(new RuntimeException("Test exception"), mongoWriteException)
            .when(vaultDocumentStorage).persistDocument(any(), any());
    final Mono<DocumentEntity> result = underTest.archiveDocument(documentArchivingContext);
    StepVerifier.create(result)
            .verifyError(MongoWriteException.class);
    verify(vaultDocumentStorage, times(2)).persistDocument(any(), any());
}