• Home
  • RSS Feed
  • Log in

Unit testing a Stripes ActionBean wired with Spring Beans
Posted by Jeroen van Wilgenburg around lunchtime: December 16th, 2008

Last friday I spent quite some time to figure out how to initialize my Spring beans when unit-testing some Stripes ActionBeans. There wasn't any Spring context at all and you really need that for integration testing.

How to set up a unit test with Stripes is explained pretty good here.
I applied approach 2, run the tests within a mock container.

My unit test is extending AbstractTransactionalJUnit4SpringContextTests and is annotated with @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration(locations = { ... })

The code I'm about to show should be put after initialzing the MockServletContext. I recommend using a method annotated with @Before where you do the initialization (I called mine setUpMockServletContext)
Since the unit tests run before the Spring config files are put in the WEB-INF directory, you have to tell Stripes where to find the configuration files:

context.addInitParameter("contextConfigLocation",
"classpath:dataSourceContext.xml\nclasspath:daoContext.xml\nclasspath:applicationContext.xml");

Stripes isn't smart enough (yet?) to read the @ContextConfiguration annotation, but this file will probably included somewhere in a parent-unit test, so it isn't a big deal.

The final step is telling Spring to initialize its context with your MockServletContext (note that this is a Stripes MockServletContext, not the one used by Spring!):

ContextLoaderListener springContextLoader = new ContextLoaderListener();
springContextLoader.contextInitialized(new ServletContextEvent(context));

That's all there is to it. Let's hope google picks up this blog and other people can handle this tiny problem quicker than I did.

Sources

* Unit testing with Spring
* Unit testing Stripes
* Using Spring with Stripes
* Stripes mailing list

  • Share/Bookmark

Tags: stripes spring unit test actionbean
Filed under Java, Testing | No Comments »



No Responses to “Unit testing a Stripes ActionBean wired with Spring Beans”



Leave a Reply

Click here to cancel reply.

Deployment automation for Java application running on Websphere, WebLogic and JBoss

Archives

  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009

Xebia Sites

  • Xebia Corporate
  • Xebia France
  • Xebia India

Categories

  • Java (282)
  • Agile (109)
  • General (50)
  • Testing (42)
  • Performance (42)
  • Hibernate (36)
  • Scrum (33)
  • Podcast (31)
  • Architecture (31)
  • Spring (28)
  • SOA (24)
  • Maven (22)
  • Project Management (22)
  • Middleware (23)
    • Deployment (14)
  • Flex (17)
  • JPA (17)
  • Eclipse (15)
  • Xebia Labs (15)
  • Quality Assurance (14)

Tag Cloud

    Scala qcon Testing Scrum Functional Programming Ajax Performance Closures Spring Semantic Web Architecture Maven Seam Lean IntelliJ Hibernate product owner Java Groovy JavaOne XML Agile Awareness Workshop Grails Xebia esb Agile Poppendieck Introduction to Agile SOA fitnesse