2

I have a Spring + JPA + Hibernate application that I'm trying to test by subclassing code>AbstractJUnit4SpringContextTests. The class allows to specify the Spring context file using the @ContextConfiguration annotation. In this file, the database is created and initialized (schema + data) using SQL scripts through the <jdbc:embedded-database> element.

Now I have two classes that load two different spring context files, but they both contain this element. If I run each test class individually, all the test methods work fine. However, if I run them from an ant script, the second test that runs will complain because the database was already initialized! This is very odd as I have fork="yes" forkmode="perTest" in my Ant JUnit target. I'm not sure why this is happening. Any ideas?

EDIT: here's a fragment of my ant script:

<junit printsummary="withOutAndErr" haltonfailure="yes" fork="yes" forkmode="perTest" maxmemory="512m">
  <classpath refid="mvn.classpath"/>
  <classpath location="${classes-core}"/>
  <classpath location="${classes-pentaho}"/>
  <classpath location="${classes-plugins}"/>
  <classpath location="${junit.classes}"/>
  <classpath location="${junit.resources}"/>
  <sysproperty key="ant.home" value="${ant.home}"/>
  <formatter type="xml"/>
  <batchtest fork="yes" todir="${junit.out.dir.xml}">
    <fileset dir="${junit.classes}" includes="**/*TestSuite.class"/>
  </batchtest>
</junit>

Thanks

Giovanni

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94

1 Answers1

0

I found a quick and elegant solution. Apparently Spring was not reinitializing the context for each test, so my H2 database was surviving somehow. I'm still a little puzzled, but adding the following to my test classes solved it:

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94