I am working on a tiny framework to wrap Database Stored Procedure calls named spwrap
@ConfineMetaClassChanges([CallableStatement])
def "Result of output parameter getInt throws SQLException" (){
given:
def sqlExceptionMsg = "exception happend while tring to call getInt"
CallableStatement.metaClass.getObject = { int parameterIndex -> throw new SQLException(sqlExceptionMsg)}
when:
def custId = customerDao.createCustomer("Abdullah", "Mohammad")
then:
def e = thrown(CallException)
e.cause == SQLException
e.cause.message == sqlExceptionMsg
}
the method createCustomer
returns no reference to CallableStatement
, however under the hood a CallableStatement.getObject(int)
is being called, and I want to test the case where a SQLException
is thrown.
I am trying to override the bahvaiour on the CallableStatement.getObject(int)
class (since I have to reference to the used object by the framework - at least in this scenario)
The above test fails as it seems the CallableStatement.getObject(int)
is not being changed. However when i use the <<
it complains (and it should). How to accomplish this?
UPDATE:
Using GroovyMock
doesn't help:
// test fails!
def "Calling interface methods calling JDBC Driver methods" (){
given:
CustomerDAO customerDAO2 = new DAO.Builder("jdbc:hsqldb:mem:customers", "sa", "").build().create(CustomerDAO);
def callableStatement = GroovyMock(JDBCCallableStatement, global: true)
when:
customerDAO2.createCustomer("Abdullah", "Mohammad")
then:
1 * callableStatement.getObject(_ as Integer)
}
Can I achieve this with other Mocking frameworks?