Announcement Announcement Module
No announcement yet.
Using Derby embedded database for testing with Maven Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Derby embedded database for testing with Maven


    First of all, I want to say thank you to the Spring team for providing embedded database support (Derby, ...).

    Unfortunately when using Derby embedded database support for JUnit tests, that are executed from Maven Surefire, a problem with shutting down (cleaning up) the Derby database shows up.

    We've several JUnit test cases that create an embedded database on loading the application context of the test case.

     <jdbc:embedded-database id="dataSource" type="DERBY">
            <jdbc:script location="classpath:sql/schema.sql"/>
            <jdbc:script location="classpath:sql/regelwerkdata.sql"/>
    Running each test case separately (e.g. from Eclipse) works without any problems. Running both from Maven Surefire (Life cycle phase test) only works for the first, and the second will fail on creating the tables (SQL DDL statements from file schema.sql).

    In my opinion, the reason for the failure comes from the Derby shutdown issued by the class DerbyEmbeddedDatabaseConfigurer that does a shutdown without dropping the database before.

    Has anyone ever used this kind of setup successfully? Any workarounds available?

    Kind regards

  • #2
    Similar problem here with HSQL.

    I've got two test classes, both loading the test application context which executes SQL scripts as shown above. But the second test class fails as the DB isn't shut down.

    How to create a new database for each test method (not just per class, which doesn't seem to work anyway)? Or how do you test using spring/hibernate/junit?


    • #3
      I am not sure why you need to create db for every test run, I personally use hsql in memory db and create it once using spring jdbc initialization

      <jdbc:initialize-database data-source="hsldbDS">
          <jdbc:script location="classpath:dbscripts/createDB.sql" />
      I have my db definition (sql to create db) and some initial data (insert sql) in createDB.sql , spring will do the rest by executing all the sql that is listed in createDB.sql


      • #4
        Found a good solution later:

        Works for me, at least one set up for each class.