• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts2: How to Test Actions by passing Interceptors

 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Friends
I have a struts application with a LoginInterceptor which checks for a session to let users perform login or bypasses this check if we are trying to make a login or registration. Also I have few other test classes which i want to test without going through the LoginInterceptor is there any way we can test just the action and by pass an interceptor (possibly mocking?)

LoginInterceptor uses Service Interface to check for a valid user in case if we are trying to login ?

Any pointers would be much appreciated



First test passes and the second fails throwing Null Pointer as the code steps into the Interceptor and does not mock interceptor any ideas why?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to just test an action without running through its interceptor stack just instantiate the action and call its methods.
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello David
Thanks for your reply, thats exactly what i am trying to do , but this login interceptor is present in struts.xml which is called before an action naturally as it is placed higher in stack. I want to unit test action with login interceptor for many URIS but for some i need to escape the login interceptor.

So i was thinking is there any way we can mock interceptor to escape it? Hope my concern is clear?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, I'd call the integration testing, not unit testing: unit testing wouldn't involve any interceptors.

Under what circumstances would you *not* want to test a given action with an interceptor that would be applied to that action in the deployed application? If you're going to integration test an action then it should be integrated realistically, otherwise what's the point?

If an action shouldn't have an interceptor applied to it that should be handled at the configuration level. If you want to test an action in isolation with no interceptors, then just instantiate it directly and call its methods directly.
 
Mohamed Farouk
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello David
Thanks very much for your reply, sorry for the late response.


Firstly, I'd call the integration testing, not unit testing: unit testing wouldn't involve any interceptors.


I would still call this unit test as I am trying to test actions mocking service layer.


Under what circumstances would you *not* want to test a given action with an interceptor that would be applied to that action in the deployed application? If you're going to integration test an action then it should be integrated realistically, otherwise what's the point?


Agree,but I was trying to test an action which is unrelated to the application (just using service layer, so hence mocking) and as interceptor was causing concern I was thinking is there any way to escape the interceptor without changing its code.


If an action shouldn't have an interceptor applied to it that should be handled at the configuration level. If you want to test an action in isolation with no interceptors, then just instantiate it directly and call its methods directly.



What is the reason we have to use actionproxy to test an action even though class under test is an action. As you said we can directly call an action class methods there by avoiding interceptors. But i was curious to find out why then we have to get an action proxy, invocation context and the rest as all the action related test examples shown in struts 2 was using them?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A unit test tests isolated functionality. If there are interceptors you're no longer testing the action--you're testing the action, the interceptors, the interception mechanism, etc. By definition that's an integration test.

I'm saying you *don't* have to go through the entire stack if all you want to do is test an action's functionality in isolation, and shouldn't.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic