Reputation: 17
Hi I am trying to mock jdbc template queryForObject
method . I am using H2 InMem Db.
Following is the error
Caused by: org.h2.jdbc.JdbcSQLException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
[90002-196]
Below is the code snippet.
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);
Upvotes: 1
Views: 464
Reputation: 12953
the problem is that you are not using a mock, so you are running when
on a real object
JdbcTemplate jdbcTemplate = new JdbcTemplate();
should be replaced with:
JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);
Upvotes: 1