Spring boot Interceptor with Unit testing
Interceptor
Configuration
Testing
You can write end to end Test as well
Note: @webmvc test will not pick up the interceptor logic, so you can test your controller test in isolation.
Thanks for sticking around, you legend! 🙌 Before you gallop into the sunset:
👏 Give that clap button a workout, it’s been feeling a bit lonely lately.
And don’t forget to follow me— I’m as cool as a penguin in shades. 😎
Got burning questions or insights hotter than freshly brewed coffee? Drop ’em in the comments section below. Let’s chat, share, and collectively become Programming wizards. 🧙♂️
Want to join forces in the Java galaxy? Let’s connect on LinkedIn, where we can network and exchange memes that only coders truly understand. 💼💻
Catch you on the flip side, fellow coder! 🚀
Reference Links
https://wheleph.gitlab.io/posts/2021-06-28-springweb-testing/https://github.com/spring-projects/spring-boot/issues/17941https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/html/boot-features-testing.html#:~:text=Test%20slices%20exclude%20%40Configuration%20classes%20from%20scanning.%20For%20example%2C%20for%20a%20%40WebMvcTest%2C%20the%20following%20configuration%20will%20not%20include%20the%20given%20WebMvcConfigurer%20bean%20in%20the%20application%20context%20loaded%20by%20the%20test%20slice%3ABean injection in interceptorhttps://stackoverflow.com/questions/45191819/avoid-controllers-initialization-when-testing-spring-boot-handlerinterceptorTesting interceptorhttps://stackoverflow.com/questions/24140494/how-to-test-spring-handlerinterceptor-mappinghttps://www.leveluplunch.com/blog/2014/07/09/how-to-test-spring-mvc-handler-interceptors/ — this is workingSource code of webmvctest.javahttps://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.javaSimple solutionhttps://stackoverflow.com/questions/49474778/how-to-mock-spring-handlerinterceptoradapter-in-an-integration-test