20 Ağustos 2023 Pazar

SpringWebFlux StepVerifier Sınıfı

Giriş
Açıklaması şöyle
When testing reactive components, ensure you cover error scenarios using StepVerifier to verify the behavior of your reactive streams in response to different types of errors.
Örnek
Şöyle yaparız
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

public class ReactiveStreamTest {
  @Test
  public void testFlux() {
    Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5);

    StepVerifier.create(numbers)
      .expectNext(1, 2, 3, 4, 5)
      .verifyComplete();
  }
  @Test
  public void testTransformations() {
    Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5);

    StepVerifier.create(numbers
      .filter(number -> number % 2 == 0)
      .map(evenNumber -> evenNumber * 2)
    )
    .expectNext(4, 8)
    .verifyComplete();
  }
  @Test
  public void testWithError() {
    Flux<Integer> numbers = Flux.just(1, 2, 3)
      .concatWith(Flux.error(new RuntimeException("Oops! An error occurred.")));

    StepVerifier.create(numbers)
      .expectNext(1, 2, 3)
      .expectError(RuntimeException.class)
      .verify();
  }
}

Hiç yorum yok:

Yorum Gönder