Hey guys. I’m Working on a Kotlin + Spring Boot 2....
# spring
o
Hey guys. I’m Working on a Kotlin + Spring Boot 2.2 app (non reactive). I recently converted my routing definition to Kotlin DSL with
WebMvc.fn
instead of using
@RequestMapping
annotations. Since I’m now using request handlers rather than controllers, my controller unit tests (
MockMvc
with standalone setup) are obviously failing 🙂 I’m trying to fix my unit tests but I’m struggling making things work. Seems like every request I’m making using
MockMvc
(without standalone steup this time) ends up with 404. When I run my app or my integration tests, everything works just fine, so I’m guessing it’s somehow related to the routes bean not being loaded to the app context. Failed to work around it though. Any ideas? am I missing something?
m
I haven't used DSL yet, but if there's no controller, wouldn't you just unit test the handlers now? The integration tests will ensure/verify the routing is defined correctly. Is there anything of value to test in the Controllers anymore?
o
I found it hard to unit test the handler on its own as I needed to deal with
ServerRequest
and
ServerResponse
objects. That’s why I thought to replace my controller unit tests with route-handler unit tests using
MockMvc
. Eventually I needed to create a custom context initializer for my tests and also use the
beans
DSL in which I register my routing bean. Once I added these beans when calling
runApplication
everything worked as expected.
s
I can't test right now but I have asked to Arjen, the Spring developer behind WebMvc.fn.
I will forward you his feedback
It is expected to be supported, could you share a repro project ?
o
@sdeleuze First of all, thanks. Unfortunately I cannot share it as it’s a private repo. Second - the issue was indeed the missing routing bean that apparently isn’t loaded if written in Kotlin DSL. I found a workaround for this by creating a custom application context initializer, as suggested here - https://stackoverflow.com/questions/51391485/how-to-use-kotlin-beans-dsl-initializer-in-springboottest?rq=1 Eventually I decided to give up on this workaround since it didn’t feel like the cleanest solution. I ended up taking Mike’s advice, unit tested my handlers and let my integration test cover the routing. Glad to hear that it is expected to be supported out of the box though 🙂