File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts2: How to Test Actions by passing Interceptors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2: How to Test Actions by passing Interceptors" Watch "Struts2: How to Test Actions by passing Interceptors" New topic
Author

Struts2: How to Test Actions by passing Interceptors

Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
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?


SCJP, SCWCD, SCBCD, SCEA 5
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jun 08, 2005
Posts: 249
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jun 08, 2005
Posts: 249
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2: How to Test Actions by passing Interceptors