Skip to main content

Testing the Databricks JDBC Driver (Simba)

note

This page applies to Databricks JDBC driver versions below version 3. For version 3 and above, see Databricks JDBC Driver.

This page describes how to test code that uses the Databricks JDBC Driver.

Use any test framework for JDBC-compatible languages. The following examples use JUnit and Mockito to test JDBC driver connections. This code is based on the example in Authentication settings for the Databricks JDBC Driver (Simba).

Helper functions

The Helpers.java file contains utility functions for working with JDBC connections:

  • CreateConnection: Opens a connection to a Databricks compute resource.
  • SelectNYCTaxis: Queries the specified number of rows from samples.nyctaxi.trips.
  • PrintResultSet: Prints the result set contents to the console.
Java
// Helpers.java

import java.sql.*;
import java.util.Properties;

public class Helpers {
static Connection CreateConnection(
String url,
Properties p
) throws SQLException {
Connection conn = DriverManager.getConnection(url, p);
return conn;
}

static ResultSet SelectNYCTaxis(
Connection conn,
long rows
) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM samples.nyctaxi.trips LIMIT " + rows);
return rs;
}

static void PrintResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData md = rs.getMetaData();
String[] columns = new String[md.getColumnCount()];
for (int i = 0; i < columns.length; i++) {
columns[i] = md.getColumnName(i + 1);
}
while (rs.next()) {
System.out.print("Row " + rs.getRow() + "=[");
for (int i = 0; i < columns.length; i++) {
if (i != 0) {
System.out.print(", ");
}
System.out.print(columns[i] + "='" + rs.getObject(i + 1) + "'");
}
System.out.println(")]");
}
}
}

Main class

The Main.java file calls the helper functions to connect and query data:

Java
package org.example;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.databricks.client.jdbc.Driver");
String url = "jdbc:databricks://" + System.getenv("DATABRICKS_SERVER_HOSTNAME") + ":443";
Properties p = new Properties();
p.put("httpPath", System.getenv("DATABRICKS_HTTP_PATH"));
p.put("AuthMech", "3");
p.put("UID", "token");
p.put("PWD", System.getenv("DATABRICKS_TOKEN"));

Connection conn = Helpers.CreateConnection(url, p);
ResultSet rs = Helpers.SelectNYCTaxis(conn, 2);
Helpers.PrintResultSet(rs);
}
}

Unit tests with mocking

The HelpersTest.java file uses JUnit and Mockito to test the SelectNYCTaxis function. Mocking simulates database connections without using actual compute resources, so tests run in seconds without affecting your Databricks workspaces.

Java
package org.example;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class HelpersTest {
@Test
public void testSelectNYCTaxis() throws SQLException {
Connection mockConnection = Mockito.mock(Connection.class);
Statement mockStatement = Mockito.mock(Statement.class);
ResultSet mockResultSet = Mockito.mock(ResultSet.class);

Mockito.when(mockConnection.createStatement()).thenReturn(mockStatement);
Mockito.when(mockStatement.executeQuery(Mockito.anyString())).thenReturn(mockResultSet);

ResultSet rs = Helpers.SelectNYCTaxis(mockConnection, 2);
assertEquals(mockResultSet, rs);
}
}

Because SelectNYCTaxis only runs a SELECT statement, mocking isn't strictly required here. However, mocking is especially useful when testing functions that modify data (INSERT INTO, UPDATE, DELETE FROM), as you can run tests repeatedly without affecting table state.