Sky*_*and 5 java junit hibernate jax-rs jersey
如何在测试方法之间删除,删除或回滚或断开H2数据库?
这是我的一些hibernate测试配置:
<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>
Run Code Online (Sandbox Code Playgroud)
我有一个基类,我将继承我的所有测试:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
似乎应该有一种简单的方法来执行这样的集成测试?
也许我可以使用某种某种Transactional注释?但是,我想知道如何用相对香草的球衣+ JUnit做到这一点.
泽西岛是2.12,Java 1.7,H2是1.4.181,JUnit是4.11.
版本可以协商.
我正在使用一个处理(重新)初始化的 SQL 文件。对于这样的文件,您可以使用:
import org.h2.tools.RunScript;
@Autowired
private DataSource dataSource; // get your DataSource somehow
@Before
public void setup() {
RunScript.execute(dataSource.getConnection(), new FileReader("src/test/resources/testdb/data.sql");
...
Run Code Online (Sandbox Code Playgroud)
每次测试数据库都会再次恢复正常。
但不要并行运行测试。
一种不同的 - 也许更好的方法:
| 归档时间: |
|
| 查看次数: |
1803 次 |
| 最近记录: |