我有这个单元测试,开始显示错误后,我输入这两个新的参数连接和通知。我试图使模拟,但它显示错误“无法打开事务,因为它是空”,有人能帮助我吗?
protected int getTotalRecords(Connection connection, Statement statement, final String sql, NotificationData notification) {
int rows = 0;
ResultSet rs = null;
try(ResultSet resultSet = statement.executeQuery(buildCountRecordsSQL(sql))) {
if (resultSet.next()) {
rows = resultSet.getInt("ROWS_COUNT");
}
} catch (SQLException e) {
try{
SEEntityManager.rollbackAndReOpenTransaction();
statement = connection.createStatement();
rs = statement.executeQuery(notification.getFieldSql());
return rows = BROKE_SQL;
}catch (final SQLException e1){
logger.error(e1.getMessage(), e1);
}
}
logger.debug("Total records: "+rows);
return rows;
}
测试J单位
@Test
public void verifyExceptionCountRecords() throws SQLException{
Mockito.when(resultSet.next()).thenThrow(new SQLException());
assertTrue(daoDefault.getTotalRecords(Mockito.mock(Connection.class), statement, "", notification)==0);
Mockito.verify(resultSet, Mockito.never()).getInt("ROWS_COUNT");
}
测试错误:
Cannot open transaction cause it is null
1条答案
按热度按时间dl5txlt91#
错误消息“Cannot open transaction cause it is null”可能是由代码中对
SEEntityManager.rollbackAndReOpenTransaction()
的调用引起的。此方法正在尝试打开新事务,但由于事务对象为null而无法打开。若要解决此问题,可以模拟Connection对象和Statement对象的行为。可以模拟
connection.createStatement()
方法以返回模拟语句,然后可以使用模拟语句返回模拟ResultSet
。