JUnit, H2, JaxRS, a memory-falling database between test methods

How do I delete or delete or cancel or disconnect from my H2 database between test methods?

Here is a bit of my sleep configuration:

<property name="connection.driver_class">org.h2.Driver</property> <property name="connection.url"> jdbc:h2:mem:test;DB_CLOSE_DELAY=0;INIT=RUNSCRIPT FROM './h2-ext/add_to_date.sql' </property> <property name="hbm2ddl.auto">create</property> 

I have a base class inheriting all my tests:

 public abstract class BaseTest { @After public void drop() throws Exception { System.out.println("!!!!!! DROP !!!!!!&&&&&&&&&&&&&&&&&&&&&&"); // this seems to do nothing //org.h2.store.fs.FileUtils.deleteRecursive("mem:test", true); //org.h2.store.fs.FileUtils.deleteRecursive("NoTellingwhatThisdoes", true); // the schema does not get recreated, it seems //Session sess = DB.sf.openSession(); //sess.createSQLQuery("DROP ALL OBJECTS;").executeUpdate(); // org.h2.jdbc.JdbcSQLException: Cannot truncate "PUBLIC.FOO"; //Session sess = DB.sf.openSession(); //sess.createSQLQuery("TRUNCATE TABLE FOO;").executeUpdate(); //sess.createSQLQuery("TRUNCATE TABLE BAR;").executeUpdate(); } } 

It seems like there should be an easy way to perform such an integration test?

Maybe I can use some Transactional annotation? But I would like to know how to do this with a relatively vanilla jersey + JUnit.

Jersey - 2.12, Java 1.7, H2 - 1.4.181, JUnit - 4.11.

Versions are negotiable.

+5
source share

Source: https://habr.com/ru/post/1205470/


All Articles