我有以下单元测试,其中我想测试以正确的顺序用正确的参数调用该语句。
StoredProcedure sp = new StoredProcedure("Schema.Procedure(?,?,?)");
CloseableSQLServerDataTable table = new CloseableSQLServerDataTable(new SQLServerDataTableFactory().build());
Connection conn = Mockito.mock(Connection.class);
SQLServerCallableStatement stmt = Mockito.mock(SQLServerCallableStatement.class);
when(conn.prepareCall("{ call Schema.Procedure(?,?,?) }")).thenReturn(stmt);
sp.executeWithTableParam(conn, table, 1, "Schema.CustomType", "foo", true);
InOrder inOrder = Mockito.inOrder(stmt);
inOrder.verify(stmt).setObject(1, "foo");
inOrder.verify(stmt).setObject(2, table);
inOrder.verify(stmt).setObject(3, true);
inOrder.verify(stmt).execute();
方法executeWithTableParam
仅获取语句并按正确的顺序进行参数设置。
这是一个摘录:
try (CallableStatement stmt = conn.prepareCall(call))
{
setParametersWithTable(stmt, tableData, tableParamIndex, tableDataType, parameters);
stmt.execute();
}
最后是setParametersWithTable
方法
for (int index = 0; index < length; index++)
{
if (index < tableParamIndex)
{
stmt.setObject(index + 1, parameters[index + 1]);
}
else if (index == tableParamIndex)
{
((SQLServerCallableStatement) stmt).setStructured(tableParamIndex + 1 , tableDataType, tableData.getWrapped());
}
else
{
stmt.setObject(index + 1, parameters[index + 1]);
}
}
}
我的问题是,为什么在setMethod上会出现NullPointerException?不应执行SQLServerStatement类中的代码,因为这就是进行模拟的目的
java.lang.NullPointerException
at com.microsoft.sqlserver.jdbc.SQLServerStatement.checkClosed(SQLServerStatement.java:1046)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObject(SQLServerPreparedStatement.java:1487)
at com.xxx.yyy.StoredProcedure.setParametersWithTable(StoredProcedure.java:225)
相关版本: