Proper testing pipeline
See original GitHub issueHello,
I’m in the process of evaluating Javalin in Java to replace a Spring service that is in the making. So far I’m excited about the simplicity but power and control it has.
I am wondering on how to properly unit test the controllers. In Spring, there is the possibility to “mockMvc”.
In Javalin, I am testing the controllers’ handle(ctx)
.
Unfortunately, this does not work very well, because some classes are not mockable, e.g. the result from ctx.validatedBodyAsClass(...)
.
Alternatively, I could also mock HttpServletRequest with mockito. But this would result in mocking-hell, because I would have to mock every sub-object, like the writers, etc…
Or create a complete HttpServletRequest-implementation for testing.
How are you unit-testing your controllers? Do you have guidelines and examples in Java?
Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
If you can write your tests in Kotlin that would be a great solution. I don’t remember if I added any mock tests to the repo. I should probably write a tutorial.
IMO, yes. You could argue that a lot functions could be moved into
Request
andResponse
classes on theContext
, but that would instantly add a lot of noise to controllers (ex: callingctx.request().pathParams(":user-id")
). You could also make an interface which takes aContext
, aRequest
, and aResponse
, but that would similarly create a lot of noisy method signatures and lambdas.I went through the different alternatives when designing the API, and this was found to be the best solution.
I’m worried that by introducing single implementation interfaces, the code will be harder to read and understand for new users and new contributors (this also worries me about nesting functionality into different objects on the
Context
).