Add web client tests

This commit is contained in:
Sambo Chea 2020-07-11 21:19:29 +07:00
parent 45b6f104fb
commit a8b2346b16
2 changed files with 28 additions and 3 deletions

View File

@ -2,8 +2,6 @@ package com.cubetiqs.wsclient
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Flux
import reactor.kotlin.extra.retry.retryExponentialBackoff
import java.time.Duration
class WebClientStockClient(val webClient: WebClient) : StockClient {
override fun priceFor(symbol: String): Flux<StockPrice> {
@ -11,7 +9,6 @@ class WebClientStockClient(val webClient: WebClient) : StockClient {
.uri("http://localhost:8080/stocks/{symbol}", symbol)
.retrieve()
.bodyToFlux(StockPrice::class.java)
.retryExponentialBackoff(5, Duration.ofSeconds(10), Duration.ofSeconds(20))
.doOnError {
println(it.message)
}

View File

@ -0,0 +1,28 @@
package com.cubetiqs.wsclient
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import reactor.core.publisher.Flux
import reactor.test.StepVerifier
@SpringBootTest
class WebClientApplicationTests @Autowired constructor(
private val stockClient: WebClientStockClient
) {
@Test
fun shouldRetrieveStockPricesFromTheService() {
val prices: Flux<StockPrice> = stockClient.priceFor("USD")
Assertions.assertNotNull(prices)
val fivePrices = prices.take(5)
Assertions.assertEquals(5, fivePrices.count().block())
Assertions.assertEquals("USD", fivePrices.blockFirst()?.symbol)
StepVerifier.create(prices.take(2))
.expectNextMatches { it.symbol == "USD" }
.expectNextMatches { it.symbol == "USD" }
.verifyComplete()
}
}