28 Nisan 2023 Cuma

SpringWebFlux WebClient Test

Giriş
Açıklaması şöyle
Basically, there are two general directions for writing tests for the reactive chain:

1. Directly call the methods to test and receive the outcome by invoking block() on the resulting Mono or Flux.
2. Wrap these calls with the StepVerifier class provided by the Reactor framework.
StepVerifier
Bir Flux veya Mono nesnesini test etmek içindir. Testing sonunda verifyComplete() veya bir türevinin çağrısını yapmak gerekir

assertNext metodu
Örnek
Şöyle yaparız
@Test
public void verifyGetByNameReturnsEntryForSuccessfulRequestUsingStepVerifier() {
    InventoryEntry expectedEntry = new InventoryEntry(42, "...", 5);

    mockBackEnd.enqueue(assembleResponse(expectedEntry));

    StepVerifier
            .create(cut.getByName(expectedEntry.name()))
            .assertNext(entry -> assertThat(entry).isEqualTo(expectedEntry))
            .verifyComplete();
}
expectNext metodu
Örnek
Şöyle yaparız
@Test
public void testFluxStream() {
  Flux<Integer> flux = Flux.just(1, 2, 3, 4, 5);

  StepVerifier.create(flux)
    .expectNext(1)
    .expectNext(2)
    .expectNext(3)
    .expectNext(4)
    .expectNext(5)
    .verifyComplete();
}

Hiç yorum yok:

Yorum Gönder