SpringBoot WebFlux Test – @WebFluxTest

In the tutorial, JavaSampleApproach will show you how to work with SpringBoot WebFlux Tests using @WebFluxTest.

Related posts:
SpringBoot WebFlux Annotation-based RestAPIs
Spring WebClient with Spring Webflux RestAPIs | SpringBoot 2

I. Technologies

– Java: 1.8
– Maven: 3.3.9
– Spring Tool Suite: Version 3.9.0.RELEASE
– Spring Boot: 2.0.0.M4
– Spring Boot Starter Webflux

II. SpringBoot WebFlux Tests

@WebFluxTest helps to test Spring WebFlux controllers with auto-configuring the Spring WebFlux infrastructure, limit scanned beans like {@Controller, @ControllerAdvice, @JsonComponent, WebFluxConfigurer} and never scan @Component beans.

@WebFluxTest will auto-configures WebTestClient to quickly test WebFlux controllers without starting a full HTTP server.

III. Practice

We create a SpringBoot project as below:

SpringBoot WebFlux test - project structure

Step to do:
– Create SpringBoot project
– Define data model
– Implement WebFlux APIs
– Implement Testcases
– Run and check results

1. Create SpringBoot project

– Using SpringToolSuite to create a SpringBoot project with spring-boot-starter-webflux dependency:

2. Define data model

– Define a Customer model:

3. Implement WebFlux APIs

Refer at SpringBoot WebFlux Annotation-based RestAPIs

– Implement a WebFlux RestControllerAPIs:

4. Implement Testcases

5. Run and check results

– Run unit-test as below image:

SpringBoot WebFlux test - run unitest

-> Results:

SpringBoot WebFlux test - results

IV. Sourcecode

SpringWebFluxTest

By grokonez | October 4, 2017.



Related Posts


1 thought on “SpringBoot WebFlux Test – @WebFluxTest”

  1. Hi,

    Thanks for the detailed tutorial.
    I am getting below error on test cases.

    java.lang.AssertionError: Status expected: but was:

    > POST /optimize
    > WebTestClient-Request-Id: [1]
    > Content-Type: [application/json]

    < 200 OK
    < Content-Type: [application/json;charset=UTF-8]

    Content not available yet

    at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:197)
    at org.springframework.test.web.reactive.server.StatusAssertions.assertStatusAndReturn(StatusAssertions.java:201)
    at org.springframework.test.web.reactive.server.StatusAssertions.isCreated(StatusAssertions.java:71)
    at com.yantriks.yso.api.SourcingOptimizerAPITests.shouldSuccessfullyReturnSourcingOptimizerResult(SourcingOptimizerAPITests.java:139)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Got Something To Say:

Your email address will not be published. Required fields are marked *

*