我有一些方法,我正在尝试进行单元测试。这些方法使用Spring JdbcTemplates对DB2 v8运行良好(在生产中)。
每个SQL通过在语句中附加“WITH UR”来允许“脏读”。 另外,它们中的一些使用“限制”来添加例如“FETCH FIRST 1行只”。
这对真正的DB2工作正常,但我想对内存数据库进行单元测试 - 这就是H2进入的地方。
如果我删除“WITH UR”和“FETCH FIRST ..”,一切正常,但我不想更改方法,只需更改后端数据库。
据我所知,这不是直接使用H2,因为语法不同(虽然我使用MODE = DB2)。
现在,该怎么办?我应该/可以使用另一个内存数据库吗?我不想改变方法,也不想添加“测试”功能/黑客,所以这是不行的。
想法和同样非常感谢!
修改
我不确定它是我还是什么,但我得到以下错误。注意,我使用的是Spring 3.1和H2 1.3.166,数据库的url是“jdbc:h2:〜/ testdb; MODE = DB2”。我不知道它为什么会失败,因为sql在H2控制台中运行时有效,但不是来自我的单元测试(猜测它不是H2坏了):
Tests run: 3, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.109 sec <<< FAILURE!
getAdvisor(impl.AdvisorServiceDaoImplTest) Time elapsed: 0.078 sec <<< ERROR!
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * FROM ADVISOR FETCH FIRST 1 ROWS ONLY ]; nested exception is org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT * FROM ADVISOR FETCH[*] FIRST 1 ROWS ONLY "; SQL statement:
SELECT * FROM ADVISOR FETCH FIRST 1 ROWS ONLY [42000-166]
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:233)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:603)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:702)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:178)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:185)
at impl.AdvisorServiceDaoImpl.getAdvisor(AdvisorServiceDaoImpl.java:150)
at impl.AdvisorServiceDaoImplTest.getAdvisor(AdvisorServiceDaoImplTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT * FROM ADVISOR FETCH[*] FIRST 1 ROWS ONLY "; SQL statement:
SELECT * FROM ADVISOR FETCH FIRST 1 ROWS ONLY [42000-166]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.message.DbException.getSyntaxError(DbException.java:181)
at org.h2.command.Parser.getSyntaxError(Parser.java:484)
at org.h2.command.Parser.prepareCommand(Parser.java:233)
at org.h2.engine.Session.prepareLocal(Session.java:415)
at org.h2.engine.Session.prepareCommand(Session.java:364)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1111)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:71)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:266)
at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:245)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:581)
... 40 more
编辑#2。编辑#2
如果有人有同样的问题,这是我发现的。我使用以下内容:
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:sql/create_schemas.sql"/>
<jdbc:script location="classpath:sql/create_tables.sql"/>
<jdbc:script location="classpath:sql/create_test_data.sql"/>
</jdbc:embedded-database>
这意味着Spring将使用通用bean重新实例化数据源,因此我的设置丢失了。解决方案(在我的例子中)是简单地使用以下代码以编程方式加载数据库:
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setName("salgdb").setType(EmbeddedDatabaseType.H2)
.addScript("sql/create_schemas.sql")
.addScript("sql/create_tables.sql")
.addScript("sql/create_test_data.sql")
.build();
答案 0 :(得分:5)
您可以做的是为H2数据库提供补丁,以支持“WITH UR”功能: - )
“FETCH FIRST 1 ROWS ONLY”已经适用于最新版本的H2,但仅适用于DB2模式。要启用DB2模式,请将;MODE=DB2
附加到数据库URL,如:
jdbc:h2:~/data/test;mode=db2
示例:
create table customer(id int);
select * from customer fetch first 1 rows only;
我已经使用H2版本1.3.166进行了测试,但它也适用于旧版本,很可能是1.3.161和更新版本。要在Spring之外测试它:
jdbc:h2:~/temp/testdb;MODE=DB2
运行语句:
create table ADVISOR(id int);
SELECT * FROM ADVISOR FETCH FIRST 1 ROWS ONLY;
如果我这样做,我不会得到例外。
答案 1 :(得分:1)
从版本1.3.175开始H2也应支持“WITH UR”。 H2 changelog