rapt
rapt

Reputation: 12240

Simulating running an HTTP request on a Struts action

I'm trying to convert to JSF and Spring a web application that was written using Struts and OpenSymphony (which is a dead project by now).

The problem with that web project is that the Struts actions were written with a lot of code within them that directly goes and and gets the data for the view (the resulting JSP). Many times they also have their Struts actions extending other actions, so it's really hard to track what logic is executing when I call some action with some form.

So if I want to get this data in my JSF managed bean, I see two options:

  1. To figure out in each case exactly what happens within that hierarchy of actions, and recreating that code in some UI service (framework independent), and then call that service from my managed bean.

  2. In my UI service, to simulate somehow the operation of the HTTP request on that action, get that data, and then call that service from my managed bean.

(1) is a lot of work which I don't want to do now, maybe in the future. (2) is much faster but I'm not sure how to do it in the case of Struts.

If I try to do something like:

SomeAction Action = new SomeAction();
action.execute();

This does not usually work... since sometimes the Actions that SomeAction inherits from, would obtain the request and get data from it. So one way to overcome it is by doing:

HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();

SomeAction Action = new SomeAction();

action.setServletRequest(request);
action.execute();

This still does not work since sometimes the Actions that SomeAction inherits from, would create some objects that later would be used by SomeAction.

Is there a way to simply simulate the submission of the current request on a specific Struts action, and do it exactly how that the Struts container does it, so I would get all the intended results of that action?

I guess things like that can be done in test classes, how do I do it for Struts?

Upvotes: 1

Views: 384

Answers (1)

maple_shaft
maple_shaft

Reputation: 10463

Since you are converting to Spring, you might find the following Spring aspect interesting:

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/mock/web/package-summary.html

Here they provide you with fully mocked HttpSession, HttpRequest and HttpResponse objects so that you can not only unit test legacy Struts actions but also you can use these to help in a large refactoring effort.

Upvotes: 0

Related Questions