Re: Unit testing Actions
i've had some success unit testing struts actions using cactus. here's what i did: 0) replicate your approot in the test webapp, including struts-config.xml 1) in the web.xml (or webapp file for websphere test environment), add struts initialization parameters to the cactus ServletRedirector servlet 2) write a class that extends org.apache.commons.cactus.ServletTestCase. a skeletal example is below. i'm interested to hear feedback on this approach. overkill, not enough? as it stands, i have to create an ActionServlet and init() it with the config object i get for free from cactus (which because of step 1 has the config params that struts needs). seems kind of wacky. public class SomeActionTest extends ServletTestCase { private ActionServlet servlet; private Action action; public SomeActionTest(String name) { super(name); } public static Test suite() { return new TestSuite(SomeActionTest.class); } public static void main(String[] args) { new junit.swingui.TestRunner().run(SomeActionTest.class); } protected void setUp() throws Exception { super.setUp(); this.servlet = new ActionServlet(); servlet.init(config); this.action = new SomeAction(); action.setServlet(servlet); } protected void tearDown() throws Exception { servlet.destroy(); super.tearDown(); } public void testPerform() { try { ActionForward forward = action.perform( servlet.findMapping("/produceAForm"), null, request, response); assertEquals("success", forward.getName()); ActionFormBean formBeanDef = servlet.findFormBean("aForm"); Object obj = request.getAttribute("aForm"); assertNotNull(obj); assertEquals(formBeanDef.getType(), obj.getClass().getName()); } catch (Exception e) { fail("threw: " + e); } } } >>> [EMAIL PROTECTED] 07/31/01 04:04AM >>> Hi John, If you don't get any answer on the struts-user mailing list please repost on the cactus-user list, I know for sure that several persons have done it and would be able to give you the code. Basically, there is no helper class for the time being, meaning that before calling your action, you'll need to create needed Struts objects. But as Struts is very well designed and is using the "composition" approach (meaning that objects are not hard-linked with each other but rather you initialize an object by passing it needed domain objects) there is no problem with that and it works fine. -Vincent - Original Message - From:John Yu To:[EMAIL PROTECTED] Sent: Tuesday, July 31, 2001 8:31 AM Subject: Unit testing Actions Is there any sample showing how to do unit testing for Actions? How do I use Cactus to do it? Thanks. -- John Yu Scioworks Technologies e: [EMAIL PROTECTED] w: +(65) 873 5989 w: http://www.scioworks.com http://www.scioworks.comhttp://www.scioworks.com m: +(65) 9782 9610
mock objects for unit testing struts actions
i would like to write JUnit tests for my struts Action classes. these would test that certain attributes have been added to the servlet request, an appropriate ActionForward object is returned, and so forth. i looked into the MockObjects library on sourceforge...but it seems that a lot of the methods on the MockHttpServletRequest are no-ops or return null. am i to understand that i must subclass MockHttpServletRequest and add in whatever stubbed-out functionality i want? or are there other "mock objects" libraries for the servlet API that will better suit my needs? cheers, p
Re: not finding property on a form bean
thanks for your reply. actually, my actions were configured ok. when i browse the URL: http://localhost:8080/commissions/genTklsDailyForm.do it should invoke the action /genTklsDailyForm, which will place a form bean named tklsDailyForm into request scope, and forward to /jsp/tklsDailyForm.jsp. this JSP is to display a select list of date ranges. the action tklsDaily.do is what should be invoked when the form is submitted. i fiddled around with the action class a bit, and finally ended up getting the error: cant remove attribute from request scope when org.apache.struts.taglib.html.FormTag.doEndTag() was called. searching the archives, i discovered this is a problem with the version of jasper used by the WebSphere test environment in my IDE (VAJ 3.5.3), but that a possible workaround is to patch FormTag. after patching FormTag, my page rendered the way i expected. great list, i appreciate the response! cheers, p >>> [EMAIL PROTECTED] 07/20/01 03:12PM >>> If you really want to invoke the action associated with "genTklsDailyForm.do" then the action of your form must not be action="tklsDaily.do", but rather action="/genTklsDailyForm.do" (take note of the slash). Then your action-mapping should be: ... This may not be exactly what you want, but it should work... I would recomend taking a closer look at how to specify an action in your struts-config.xml fileyou may want to specify the scope... Hope this helps, Troy - Original Message - From: "Paul Holser" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Friday, July 20, 2001 12:11 PM Subject: not finding property on a form bean new to struts, bear with me...8^) i'm having difficulty making a particular form bean's properties available to a JSP. scenario: ...requesting genTklsDailyForm.do ...the TicketlessDailyReportFormAction puts a com.blah.TicketlessDailyReportForm bean into request scope ...then forwards to /jsp/tklsDailyForm.jsp ...error is: No getter method available for property selectedDateRange for bean under name org.apache.struts.taglib.html.BEAN any hints? my JSP: <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> Commissions: Ticketless Daily Report Form TicketlessDailyReportForm has these methods: public String getSelectedDateRange() public void setSelectedDateRange(String selectedDateRange) public String[] getDateRanges() relevant synopsis of my struts-config.xml: ...
not finding property on a form bean
new to struts, bear with me...8^) i'm having difficulty making a particular form bean's properties available to a JSP. scenario: ...requesting genTklsDailyForm.do ...the TicketlessDailyReportFormAction puts a com.blah.TicketlessDailyReportForm bean into request scope ...then forwards to /jsp/tklsDailyForm.jsp ...error is: No getter method available for property selectedDateRange for bean under name org.apache.struts.taglib.html.BEAN any hints? my JSP: <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> Commissions: Ticketless Daily Report Form TicketlessDailyReportForm has these methods: public String getSelectedDateRange() public void setSelectedDateRange(String selectedDateRange) public String[] getDateRanges() relevant synopsis of my struts-config.xml: ...