简体   繁体   中英

How to use efficient java in memory database to mock Oracle database

I have a lot of DAO tests which access the database. I tried to mock them using Java in memory database like H2. But the problem is the ddl scripts contain partition statements which does not seem to be supported by H2. I basically want to use the same ddl scripts as in production and do not want to modify them in any manner for my "unit" tests. I also tried HSQL, but found H2 more closely resembles oracle, but even then it doesnt support partition. Is there a workaround for this ?

I did look at the following Create an in-memory database structure from an Oracle instance It talks about using H2 but I have already mentioned its shortcomings. Looking for some alternatives.

如果要进行有意义的测试,则需要针对与在生产环境中运行的数据库版本相同的实例运行它们。

You could use Oracle's data dictionary to generate a H2 schema containing tables, their relationships, indices, constraints, etc. I am using such a generated schema for tests on an in-memory H2 database.

For example to get all tables and their columns, you can use this query:

SELECT 
  ut.table_name,
  utc.column_name,
  utc.data_type,
  utc.data_length,
  utc.data_precision
  FROM user_tables ut JOIN user_tab_columns utc
    ON ut.table_name = utc.table_name;

Similar queries can be made for foreign keys, other constraints, indices, sequences and all other things you need for your tests. What you further need is a templating engine to transform the results of the queries into an H2 DDL script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM