Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test spring mvc interceptors without controller class

I am trying to write a unit test for one of the interceptors in my spring-mvc rest application. I am using:

- TestNG 6.8.7
- Mockito 1.9.5
- Spring 4.0.0

Is it mandatory to have controllers to unit test the interceptors? Can we write a unit test without writing controller?

Thanks,NN

like image 833
Niranjan Avatar asked Mar 26 '26 13:03

Niranjan


1 Answers

Assuming you mean HandlerInterceptor,

Is it mandatory to have controllers to unit test the interceptors?

No. A unit test is meant to be as self contained as possible. If you're unit testing the HandlerInterceptor implementation, the only things involved are your implementation and all of its dependencies.

Can we write a unit test without writing controller?

Yes. HandlerInterceptor is just an interface with three (3) methods. None of those methods require a controller, so you don't need a controller.

like image 174
Sotirios Delimanolis Avatar answered Mar 29 '26 02:03

Sotirios Delimanolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!