Spring Boot WebFlux


Unit Test

Simple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@WebFluxTest(controllers = [OrderController::class])
class OrderControllerTest {

@Autowired
lateinit var client: WebTestClient

@Test
fun shouldPlaceLimitOrder() {
client.post()
.uri(RestfulPath.addOrderPath)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.body(Mono.just(limit), ReqOrder::class.java)
.exchange()
.expectStatus()
.isOk
}
}

Filter Configration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@WebFluxTest(controllers = [OrderController::class])
class OrderControllerTest {

@Autowired
lateinit var client: WebTestClient

@Autowired
private lateinit var context: ApplicationContext

@Test
fun shouldPlaceLimitOrder() {
val serverSpec = WebTestClient.bindToApplicationContext(context)

serverSpec.webFilter<Nothing>(AuthFilter())
val webTestClient = serverSpec.build()

val limit = TestingHelper.getLimit(
limitPrice = "1000.00",
quantity = "1",
contractId = Contract.BTCUSD.contractId
)

webTestClient.post()
.uri(RestfulPath.addOrderPath)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.body(Mono.just(limit), ReqOrder::class.java)
.exchange()
.expectStatus()
.isOk
}
}

Reference

https://grokonez.com/testing/springboot-webflux-test-webfluxtest
https://www.callicoder.com/spring-5-reactive-webclient-webtestclient-examples/