struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Wes Wannemacher <>
Subject Re: Struts 2.1.6 Test Examples
Date Wed, 13 May 2009 19:20:27 GMT
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 -

public class SecurityManagerImplTest {

    private SecurityManager<AppUserEntity,AppRoleEntity> sm ;

    @Autowired @Qualifier("appUserDao")
    private GenericDao<AppUserEntity,String> appUserDao ;

    @Autowired @Qualifier("appRoleDao")
    private GenericDao<AppRoleEntity,String> 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 Wannemacher
Author - Struts 2 In Practice
Includes coverage of Struts 2.1, Spring, JPA, JQuery, Sitemesh and more

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message