Spring boot Interceptor with Unit testing

Pradeep Nooney
1 min readMay 28, 2022

--

Interceptor

https://ray.so/?title=&theme=breeze&spacing=32&background=true&darkMode=true&code=aW1wb3J0IGxvbWJvay5Ob25OdWxsOwppbXBvcnQgb3JnLnNwcmluZ2ZyYW1ld29yay5iZWFucy5mYWN0b3J5LmFubm90YXRpb24uQXV0b3dpcmVkOwppbXBvcnQgb3JnLnNwcmluZ2ZyYW1ld29yay53ZWIuc2VydmxldC5IYW5kbGVySW50ZXJjZXB0b3I7CgppbXBvcnQgamF2YXguc2VydmxldC5odHRwLkh0dHBTZXJ2bGV0UmVxdWVzdDsKaW1wb3J0IGphdmF4LnNlcnZsZXQuaHR0cC5IdHRwU2VydmxldFJlc3BvbnNlOwoKCnB1YmxpYyBjbGFzcyBMb2dnaW5nSW50ZXJjZXB0b3IgaW1wbGVtZW50cyBIYW5kbGVySW50ZXJjZXB0b3IgewoKICAgIEBBdXRvd2lyZWQKICAgIHByaXZhdGUgVGhpcmRQYXJ0eVNlcnZpY2VCZWFuIHRoaXJkUGFydHlTZXJ2aWNlQmVhbjsKCiAgICBAT3ZlcnJpZGUKICAgIHB1YmxpYyBib29sZWFuIHByZUhhbmRsZShATm9uTnVsbCBIdHRwU2VydmxldFJlcXVlc3QgcmVxdWVzdCwgQE5vbk51bGwgSHR0cFNlcnZsZXRSZXNwb25zZSByZXNwb25zZSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICBATm9uTnVsbCBPYmplY3QgaGFuZGxlcikgewogICAgICAvL3lvdXIgbG9naWMKCiAgICAgICAgcmV0dXJuIHRydWU7CiAgICB9Cn0&language=java

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

--

--

Pradeep Nooney
Pradeep Nooney

Written by Pradeep Nooney

Senior Full Stack Developer at Sanofi

No responses yet