title: Spring MVC - Cleaning the database between tests using Flyway
body: <p>I use Flyway to manage the state of my DB in my Spring MVC application. </p>
  
  <p>I have it configured in my servlet context XML file exactly as the recommend in <a href="https://flywaydb.org/documentation/api/" rel="noreferrer">their docs</a></p>
  
  <pre><code>&lt;bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate"&gt;
      &lt;property name="dataSource" ref="..."/&gt;
      ...
  &lt;/bean&gt;
  
  &lt;!-- The rest of the application (incl. Hibernate) --&gt;
  &lt;!-- Must be run after Flyway to ensure the database is compatible with the code --&gt;
  &lt;bean id="sessionFactory" class="..." depends-on="flyway"&gt;
      ...
  &lt;/bean&gt;
  </code></pre>
  
  <p>I'd like to do two things in my JUnit tests -</p>
  
  <ol>
  <li><p>Once, before ALL tests, drop and recreate the database and let it re-migrate. This creates a clean database for each test suite. </p></li>
  <li><p>Before each test, clean all the DB tables. In other frameworks (e.g. RSpec/Rails) I've accomplished this by running DB statements transactionally so they rollback at the end of the test. Not sure what the best practice is in the Spring MVC world.</p></li>
  </ol>
  
  <p>I have no clue where to really begin with implementing the above, so any guidance is appreciated.</p>
  
  <p>Thanks!</p>