Hi guys! I'm currently working on a REST applicati...
# server
b
Hi guys! I'm currently working on a REST application and try to do that with spring boot, webflux and the functional way with kotlin. With Annotations one could write unit tests by mocking the repository and call the methods of the controller. As the controller returns a Mono of the desired object, it was easily compared and asserted. In the functional way, my handler returns a Mono<ServerResponse>. When I debug it, the desired object is in that ServerResponse, but how to I get it's body? The interface doesn't offer a method for that?! Any help is appreciated! 🙂
t
Maybe ask in #test
l
@Ben Spring has a class called WebTestClient which can be used for testing this type of controllers. It has some issues with generics in Kotlin, but it should work for basic usage. Here's some samples. https://github.com/sdeleuze/webflux-kotlin-web-tests
b
@Lucas I'm already using the WebTestClient, but for me that's an integration test which goes onto the correct . Right now I'm trying to write Unit tests for the handler, just like I did for the controller before doing it the functional way. @tddmonkey Thanks, I'll do that 🙂
s
@Ben I have updated https://github.com/sdeleuze/webflux-kotlin-web-tests with latest guidelines (Mockk instead of Mockito, JUnit 5) and have added an example of true mocked test (no application context is runned), see https://github.com/sdeleuze/webflux-kotlin-web-tests/blob/master/src/test/kotlin/com/example/RouterMockedTest.kt
b
@sdeleuze This is very helpful, thank you very much! 👍
@sdeleuze One more question about testing functional code: I don't have a RouteConfiguration class, my router is just a function and added in the initialization of the application as a bean. So I can't use the @WebFluxTest with the @Import annotation. As a workaround I'm using the @SpringBootTest annotation, but is that really the correct way?
s
That will work but that will be slower son not recommanded. Could you share how your router is declared ?