Spring + JUnit + H2 + JPA : Is it possible to drop-create the database for every test?

前端 未结 3 1217
孤独总比滥情好
孤独总比滥情好 2021-01-04 23:46

To keep the independency between the JUnit tests, I need to create the database at the beginning of every test, and destroy it at the end of every test.

The database

3条回答
  •  醉话见心
    2021-01-04 23:52

    You should probably use JPA or Spring features, but just for completeness:

    On the database side, H2 supports running init scripts when opening the database as follows:

    jdbc:h2:mem:test;INIT=runscript from '~/create.sql'
    

    or

    jdbc:h2:mem:test;INIT=runscript from 'classpath:/com/acme/create.sql'
    

    And when you close in-memory databases (like the example above), the data is dropped. Or you could run the SQL statement drop all objects.

提交回复
热议问题