TableMetadata { List<String> getColumnNames(String tableName) throws SQLException { List<String> result = Lists.newArrayList(); try ( Connection connection = connectionProvider.getConnection(); PreparedStatement stmt = connection.prepareStatement(GET_COLUMN_NAMES_QUERY)) { stmt.setString(1, tableName); ResultSet rs = stmt.executeQuery(); while (rs.next()) { result.add(rs.getString(1)); } return result; } catch (SQLException sqle) { throw new DataAccessException(COLUMN_NAMES_ERROR + tableName, sqle); } } TableMetadata(ConnectionProvider connectionProvider);   }
@Test(groups = SMALL) public void testGetTableColumnNamesUsesConnection() throws SQLException { tableMetadata.getColumnNames(TEST_TABLE_NAME); verify(mockedConnectionProvider).getConnection(); verify(mockedConnection).prepareStatement(anyString()); verify(mockedStmt).executeQuery(); }
@Test(groups = SMALL) public void testGetTableColumnProperlyClosesConnection() throws SQLException { tableMetadata.getColumnNames(TEST_TABLE_NAME); verify(mockedConnectionProvider).getConnection(); verify(mockedStmt).close(); verify(mockedConnection).close(); }
@Test(groups = INTEGRATION) public void testGetColumnNamesForExistingTable() throws SQLException { tableMetadata = new TableMetadata(new MysqlConnectionProvider(URL, USERNAME, PASSWORD)); List<String> columnNames = tableMetadata.getColumnNames(TEST_TABLE_NAME); assertEquals(columnNames.size(), 2); assertTrue(columnNames.contains("ID")); assertTrue(columnNames.contains("TXT")); }