I did go ahead using a mock (specifically www.mockobjects.com) and it was very simple. My code is shown below - maybe someone else might find it useful some time in the future:
import com.mockobjects.servlet.MockHttpServletRequest; ... public class LoginActionTest extends TestCase {
public void testSuccessfulLogin() throws Exception { ActionContext.getContext().setSession(new HashMap()); MockHttpServletRequest req = new MockHttpServletRequest(); req.setupGetRemoteAddr("127.0.0.1"); ServletActionContext.setRequest(req); LoginAction la = new LoginAction(); la.setUsername("username"); la.setPassword("password"); assertEquals(Action.SUCCESS, la.execute()); }
-----Original Message----- From: Eric Pugh To: [EMAIL PROTECTED] Sent: 1/23/04 7:59 PM Subject: RE: [OS-webwork] Testing an action that requires client IP address
This is definitly an example of where Mocks are useful. I guess it depends on what is the "better" design.. Useing the interceptor means another class, but makes the action easy to test. If you are going to use the interceptor for another action, then definitly create the interceptor. But, if it is just used on this one action, then pass in a Mock..
The type of test you are contemplating is also an easy intro into Mock's.. That is the textbook example of when to use MockObjects.. Replace the Servlet classes.
Eric
> -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] Behalf Of > Joel Hockey > Sent: Friday, January 23, 2004 1:14 AM > To: [EMAIL PROTECTED] > Subject: [OS-webwork] Testing an action that requires client > IP address > > > Hello all, > > I'm using ww2 and would like to test a LoginAction class that I have > written. The action requires the client IP address. I > access it in the > Action by calling ServletActionContext.getRequest().getRemoteAddr(). > > I am very new to ww and would appreciate any guidance on the > easiest / best > way to handle testing this sort of thing (I.e when you access the > javax.servlet objects in an action). > > My current thoughts are: > > 1/ change my action so that the IP address is populated by an > interceptor. > This makes the action very easy to test - I just set the IP > address into the > action and call execute(). > > If I did this, I would have to cast my action within the > interceptor to be > of type LoginAction. Is this a normal thing to do? This > creates a very > strong coupling between the interceptor and action, and I > would really > prefer to keep all interceptors and actions independent. > > 2/ I could use a mock of the HttpServletRequest class and set > it into the > ServletActionContext before running LoginAction.execute(). > What are other > people using for this? I expect that mockobjects provides > help for this - I > have never used it though. > > Thanks, Joel > > _________________________________________________________________ > Protect your inbox from harmful viruses with new ninemsn > Premium. Click here > http://ninemsn.com.au/premium/landing.asp > > > > ------------------------------------------------------- > The SF.Net email is sponsored by EclipseCon 2004 > Premiere Conference on Open Tools Development and Integration > See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. > http://www.eclipsecon.org/osdn > _______________________________________________ > Opensymphony-webwork mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork
------------------------------------------------------- The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. http://www.eclipsecon.org/osdn _______________________________________________ Opensymphony-webwork mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork
_________________________________________________________________
Get less junk mail with ninemsn Premium. Click here http://ninemsn.com.au/premium/landing.asp
------------------------------------------------------- The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA. http://www.eclipsecon.org/osdn _______________________________________________ Opensymphony-webwork mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/opensymphony-webwork