31 Mart 2021 Çarşamba

SpringWebFlux @WebFluxTest Anotasyonu


@WebFluxTest Anotasyonu
Örnek
Şöyle yaparız
@WebFluxTest(controllers = {RateRestController.class})
@Tag("UnitTest")
public class RateRestControllerTest {

  @MockBean
  private RateService rateService;
  
  @Autowired
  WebTestClient webTestClient;
  
  @Test
  public void getLatestRates() throws Exception {

    // Mock return data of rate service
    when(rateService.fetchLatestRates(anyString()))    
    .thenAnswer(invocation -> {
      String baseCurrency = (String) invocation.getArgument(0);
      LocalDateTime timestamp = LocalDateTime.now();
      return Flux.just(
          new Rate(timestamp, baseCurrency, "USD", Math.random()),
          new Rate(timestamp, baseCurrency, "EUR", Math.random()),
          new Rate(timestamp, baseCurrency, "CAD", Math.random()),
          new Rate(timestamp, baseCurrency, "JPY", Math.random())
          );
    });
    
    // trigger API request to rate controller
    webTestClient.get()
    .uri("/rates/latest/GBP")
    .accept(MediaType.APPLICATION_JSON)
    .exchange()
    .expectStatus().isOk()
    .expectBody()
    .jsonPath("$").isArray()
    .jsonPath("$[0].baseCurrency").isEqualTo("GBP")
    .jsonPath("$[0].counterCurrency").isEqualTo("USD")
    .jsonPath("$[0].rate").isNumber()
    .jsonPath("$[1].baseCurrency").isEqualTo("GBP")
    .jsonPath("$[1].counterCurrency").isEqualTo("EUR")
    .jsonPath("$[1].rate").isNumber()
    .jsonPath("$[2].baseCurrency").isEqualTo("GBP")
    .jsonPath("$[2].counterCurrency").isEqualTo("CAD")
    .jsonPath("$[2].rate").isNumber()
    .jsonPath("$[3].baseCurrency").isEqualTo("GBP")
    .jsonPath("$[3].counterCurrency").isEqualTo("JPY")
    .jsonPath("$[3].rate").isNumber();    
  }
}

Hiç yorum yok:

Yorum Gönder