source: class ArchivingServiceTest {
  
      private static final byte[] CONTENT;
      @Mock
      private DocumentEntityFactory documentEntityFactory;
      @Mock
      private DocumentCreationContextFactory documentCreationContextFactory;
      @Mock
      private VaultDocumentStorage vaultDocumentStorage;
      @Captor
      private ArgumentCaptor<byte[]> documentContent;
      @InjectMocks
      private ArchivingService underTest;
  
      @Test
      void testRetryWhenErrorHappensWhilePersisting() {
  
target:         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());
      }
  
  }