Re: Struts 2.1.6 Test Examples
If you just want to test your actions in isolation then the statement is true. In a test case just create a new action object, call the setters, call the execute method, check the results. This simplistic test case really doesn't need examples but it also doesn't really feel like you have fully unit tested your actions. I wanted to be able to test my actions "in full context" which includes the Struts configuration of the actions, results, interceptor stack, and validation. Here is my write up on how you do this kind of unit testing: http://glindholm.wordpress.com/2008/06/30/unit-testing-struts-2-actions/ Doug Pham wrote: > > Hi All, > In the main page one of the bullet point is: > Easy-to-test Actions - Test Struts2 Actions directly, without resorting to > mock HTTP objects. > > I would love to see a few examples of these. > > Cheers, > Doug > -- View this message in context: http://www.nabble.com/Struts-2.1.6-Test-Examples-tp23524593p23541135.html Sent from the Struts - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2.1.6 Test Examples
On Wed, May 13, 2009 at 3:00 PM, Kawczynski, David wrote: > I'm certainly no magician, but was still able to do this. > > If your action depends on session info, implement SessionAware > and provide it with a Map of name-value pairs in the testcase. > > If your action depends on request info, implement RequestAware > and provide it with a Map of name-value pairs in the testcase. > > If everything is spring-driven, you can start Spring without a > web container via ClassPathXmlApplicationContext: > > AbstractApplicationContext springContext = > new ClassPathXmlApplicationContext("applicationContext.xml"); > > springContext.registerShutdownHook(); > > SomeAction someAction = > (SomeAction)springContext.getBean("someAction"); Or even better, you can tell JUnit to run your test with Spring's JUnit runner... Here's what I do - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath*:applicationContext-test.xml"}) public class SecurityManagerImplTest { @Autowired private SecurityManager sm ; @Autowired @Qualifier("appUserDao") private GenericDao appUserDao ; @Autowired @Qualifier("appRoleDao") private GenericDao appRoleDao; @Test public void testSomeFunctionality() { Then, you can just mark classes with @Test and you don't have to worry about directly loading a spring application context. The properties annotated with @Autowired will be autowired by type and the @Qualifier let's you specify a bean-name if autowiring by type causes trouble. -Wes -- Wes Wannemacher Author - Struts 2 In Practice Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more http://www.manning.com/wannemacher - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
RE: Struts 2.1.6 Test Examples
I'm certainly no magician, but was still able to do this. If your action depends on session info, implement SessionAware and provide it with a Map of name-value pairs in the testcase. If your action depends on request info, implement RequestAware and provide it with a Map of name-value pairs in the testcase. If everything is spring-driven, you can start Spring without a web container via ClassPathXmlApplicationContext: AbstractApplicationContext springContext = new ClassPathXmlApplicationContext("applicationContext.xml"); springContext.registerShutdownHook(); SomeAction someAction = (SomeAction)springContext.getBean("someAction"); -Original Message- From: Jim Collings [mailto:jlistn...@gmail.com] Sent: Wednesday, May 13, 2009 2:24 PM To: Struts Users Mailing List Subject: Re: Struts 2.1.6 Test Examples Ya. That sounds like a magic trick. ;-) On 5/13/09, Doug Pham wrote: > Hi All, > In the main page one of the bullet point is: > Easy-to-test Actions - Test Struts2 Actions directly, without resorting to > mock HTTP objects. > > I would love to see a few examples of these. > > Cheers, > Doug > > > > - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as Banyu - direct contact information for affiliates is available at http://www.merck.com/contact/contacts.html) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system. - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts 2.1.6 Test Examples
Ya. That sounds like a magic trick. ;-) On 5/13/09, Doug Pham wrote: > Hi All, > In the main page one of the bullet point is: > Easy-to-test Actions - Test Struts2 Actions directly, without resorting to > mock HTTP objects. > > I would love to see a few examples of these. > > Cheers, > Doug > > > > - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Struts 2.1.6 Test Examples
Hi All, In the main page one of the bullet point is: Easy-to-test Actions - Test Struts2 Actions directly, without resorting to mock HTTP objects. I would love to see a few examples of these. Cheers, Doug