• Home
  • RSS Feed
  • Log in

How to wire your FitNesse fixtures with Spring
Posted by Lars Vonk at around evening time: November 3rd, 2007

For testing my applications I like to use FitNesse and for writing my applications I tend to use Spring a lot. Since there is no way in FitNesse to override the mechanism how the Fixtures are instantiated you need to somehow load the Spring context and get the beans you want to test from the Spring context. You can of course do this by hand and retrieve the beans from the application context, but this kind of clutters your fixture code. Better is to let Spring take care of the wiring.

In order to let Spring wire your fixtures we can use the functionality offered by the AutowireCapableBeanFactory. If we create a ClassPathXmlApplicationContext we can obtain a reference to the AutowireCapableBeanFactory. The mechanism I describe here is actually also used in the AbstractDependencyInjectionSpringContextTests provided by Spring to inject you JUnit tests with Spring beans.

Here is the final code needed for injecting your fixtures:

Code to load spring context and wire fixtures:

 
public class FixtureWirer {
    private static AutowireCapableBeanFactory beanFactory;
 
    static {
        beanFactory = new ClassPathXmlApplicationContext("/applicationContext.xml").getAutowireCapableBeanFactory();
    }
 
    public static void wire(Fixture fixture) {
        // make sure you AUTOWIRE_BY_NAME otherwise Spring will complain
        // about injecting the systemUnderTest property in the superclass which is of type object.
        beanFactory.autowireBeanProperties(fixture, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    }
}
 

Code in a fixture: (in this case a SetupFixture).

 
public class ApplicationSetup extends SetUpFixture {
    private SomeRepository someRepository;
 
    @Override
    protected void setUp() throws Exception {
        FixtureWirer.wire(this);
    }
    public void setSomeRepository(SomeRepository someRepository) {
        this.someRepository = someRepository;
    }
    // rest of fixture code where you can use someRepository omitted...
}
 

Happy testing.

  • Share/Bookmark

Filed under Java | 7 Comments »



7 Responses to “How to wire your FitNesse fixtures with Spring”



    Nayan Hajratwala Says:
    Posted at: March 21, 2008 at 3:54 pm

    Great tip, however, I can’t seem to get it working. I think the problem must be in my applicationContext.xml .. can you post what should be in there?

    Should there be a bean definition for each fixture?

    Also, I’m using ColumnFixtures, for which there is no setUp() method. Should i put the call to FixtureWirer in the constructor?

    Thanks!



    Lars Vonk Says:
    Posted at: March 21, 2008 at 9:35 pm

    Hi,

    What should be in you applicationContext.xml depends on the name of you property in the Fixture. In my case where the name of the property is someRepository I would have this in my applicationContext:

    bean id=\”someRepository\” class=\”SomeRepository\”

    You don\’t need to put your fixtures in applicationContext since you are going to wire them programmatically via beanFactory.autowireBeanProperties(..).

    If there is no setup you can put them in the constructor.

    Good luck,
    Lars



    Integrating FitNesse with Spring Says:
    Posted at: March 25, 2008 at 3:56 am

    [...] Finally, we ran across How to wire your FitNesse fixtures with Spring. This seemed to hold the ticket, but something just didn’t jive. I left a question in the comments (which has since been answered, and may indeed also solve the problem), and continued the quest. [...]



    Duncan Says:
    Posted at: January 27, 2009 at 5:26 pm

    I want to only call FixtureWirer.wire(this) once in the SetUp() fixture and then when calling the class in other fixtures the spring managed items will already be initialized. I cannot see how to do this and would appreciate your advice.



    Luis Atencio Says:
    Posted at: July 16, 2009 at 9:13 pm

    Amazing post!



    Antony Says:
    Posted at: August 20, 2009 at 2:05 pm

    This really helped me out. I’ve written a small framework from it that I’m thinking of open-sourcing. Is it ok to use your code? I’ll credit it you in the javadoc :-)



    Antony Says:
    Posted at: September 3, 2009 at 10:26 am

    And here it is – works great!

    I created a library that loads spring and wires in the fixture with very little code for the developer:

    http://sithopensource.blogspot.com/2009/09/sproing-fitnesse-spring-integration.html



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

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