Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

512
Views
Reintento de prueba unitaria en Spring Webflux con respuesta simulada

Durante el reintento de prueba unitaria, la respuesta del simulacro parece almacenada en caché, o lo más probable es que esté haciendo algo mal.

Estoy tratando de solicitar algo, si ocurrió un error, vuelva a intentarlo dos veces con un retraso de 1 segundo.

 public Mono<Object> someMethod(String someParam) { return someInjectedService.doSomething(someParam) .doOnError(ex -> System.out.println(ex + ": " + System.currentTimeMillis())) .retryWhen(Retry.fixedDelay(2, Duration.ofSeconds(1)).filter(ex -> ex instanceof SomeCustomException)) .doOnSuccess(result -> doSomethingOnSuccess(result)); }

Mi prueba:

 @Test void testshouldRequestThrice_whenErrorOccurs() { // Given String someParam = "testParam"; when(someInjectedService.doSomething(someParam)) .thenReturn(Mono.error(new SomeCustomException("SomeCustomException"))) // 1st response .thenReturn(Mono.error(new SomeCustomException("SomeCustomException"))) // 2nd response .thenReturn(Mono.just("SomeValidResponse")); // 3rd valid response // When var result = testService.someMethod(someParam).block(); // Then // Initial request, followed by two retries verify(someInjectedService, times(3)).doSomething(someParam); }

aquí someInjectedService es un simulacro. Mi plan era devolver una excepción dos veces y, en la tercera solicitud, devolver una respuesta válida. Pero lo que obtengo es:

org.mockito.exceptions.verification.TooFewActualInvocations: someInjectedService.doSomething("testParam");

Se busca 3 veces: -> en shouldRequestThrice_whenErrorOccurs(test.java:138)

Pero fue 1 vez:

Si bien veo 3 impresiones del .doOnError(ex -> System.out.println(ex + ": " + System.currentTimeMillis())) , siento que la solicitud real se envía solo una vez.

Gracias de antemano,

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

someInjectedService.doSomething(...) técnicamente será llamado solo una vez.

Podría usar Mono.defer(() -> someInjectedService.doSomething(someParam)) en su lugar, para asegurarse de que el método se vuelva a llamar de manera efectiva, lo que debería hacer que su prueba pase.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!