title: Share spring container between test application and embedded tomcat
body: <p>We are using cucumber-jvm to write an integration test layer in our application.  One of the challenges we are finding is managing the database between the tests and the web application.  </p>
  
  <p>A typical scenario is that we want to persist some entities in a Given step of a scenario, then perform some actions on the user interface that may, in turn, persoist more entities. At the end, we want to clean the database. Because the cucumber-jvm tests are in one jvm and the web application is running in another jvm we cannot share a transaction (at least in a way of which I am aware) so the database must be cleaned manually.</p>
  
  <p>My initial thought was to use an Embedded Tomcat server running off of an embedded in-memory database (HSQLDB) in the same JVM as the cucumber-jvm test.  This way we might be able to share a single spring container, and by extension a single transaction, from which all objects could be retrieved.</p>
  
  <p>During my initial tests it looks like Spring gets loaded and configured twice: once when the test starts and the cucumber.xml is read, and a second time when the embedded tomcat starts and the web application reads its applicationContext.xml.  These appear to be in two completely separate containers because if I try to resolve an object in one container that is specified in the other container then it doesn't resolve.  If I duplicate my configuration then I get errors about duplicate beans with the same id.</p>
  
  <p>Is there a way that I can tell Spring to use the same container for both my test application and the embedded tomcat?</p>
  
  <p>I'm using Spring 3.2.2.GA and Embedded Tomcat 7.0.39 (latest versions of both libraries).</p>
  
  <p>Am I crazy?  Do I need to provide more technical details?  Apologies if I use some incorrect terminology.</p>
  
  <p>Thanks</p>
  
  <p>p.s. If my problem seems familiar to you and you can suggest an alternative solution to the one I am trying, please let me know!</p>