### Question:
SqlDataSourceUtils { public static Object getColumnDefaultValue(DatabaseMetaData metaData, String tableName, String columnName) throws SQLException { try (ResultSet rs = metaData.getColumns(null, null, tableName, columnName)) { if (!rs.next()) { throw new IllegalStateException("Did not find meta data for column '" + columnName + "' while checking its default value"); } return rs.getObject("COLUMN_DEF"); } } private  SqlDataSourceUtils(); static void logSqlException(SQLException e); static Long getNullableLong(ResultSet rs, String columnName); static boolean isNotNullColumn(DatabaseMetaData metaData, String tableName,
                                          String columnName); static Object getColumnDefaultValue(DatabaseMetaData metaData, String tableName,
                                               String columnName);  }### Answer:
@Test public void shouldGetDefaultValue() throws SQLException { String tableName = "data"; String columnName = "category"; ResultSet resultSet = mock(ResultSet.class); given(resultSet.getObject("COLUMN_DEF")).willReturn("Literature"); given(resultSet.next()).willReturn(true); DatabaseMetaData metaData = mock(DatabaseMetaData.class); given(metaData.getColumns(null, null, tableName, columnName)).willReturn(resultSet); Object defaultValue = SqlDataSourceUtils.getColumnDefaultValue(metaData, tableName, columnName); assertThat(defaultValue, equalTo("Literature")); }
@Test(expected = IllegalStateException.class) public void shouldThrowForUnknownColumnInDefaultValueRetrieval() throws SQLException { String tableName = "data"; String columnName = "unknown"; ResultSet resultSet = mock(ResultSet.class); given(resultSet.next()).willReturn(false); DatabaseMetaData metaData = mock(DatabaseMetaData.class); given(metaData.getColumns(null, null, tableName, columnName)).willReturn(resultSet); SqlDataSourceUtils.getColumnDefaultValue(metaData, tableName, columnName); }
@Test public void shouldHandleNullDefaultValue() throws SQLException { String tableName = "data"; String columnName = "category"; ResultSet resultSet = mock(ResultSet.class); given(resultSet.getObject("COLUMN_DEF")).willReturn(null); given(resultSet.next()).willReturn(true); DatabaseMetaData metaData = mock(DatabaseMetaData.class); given(metaData.getColumns(null, null, tableName, columnName)).willReturn(resultSet); Object defaultValue = SqlDataSourceUtils.getColumnDefaultValue(metaData, tableName, columnName); assertThat(defaultValue, nullValue()); }