您好,我正在尝试模拟 jdbc 模板 queryForObject
方法。我正在使用 H2 InMem Db。
错误如下
Caused by: org.h2.jdbc.JdbcSQLException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
[90002-196]
下面是代码片段。
EmbeddedDatabase db = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("table.sql").build();`
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(db);
jdbcTemplate.execute("insert into AlertTypeHeader values " +
"(1234,365537,'TestAT','ExtId',123,345,null,null,2,0)");
when(jdbcTemplate.queryForObject(anyString(), new Object[]{},ResultSet::getObject)).thenReturn(RequiredObject);
最佳答案
问题是您没有使用模拟,因此您在真实对象上when
运行
JdbcTemplate jdbcTemplate = new JdbcTemplate();
应替换为:
JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);
关于java - 使用 H2 db 模拟 Jdbctemplate 会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58165804/