testcontainers/testcontainers-java · error · NoDriverFoundException

Could not get Driver

Error message

Could not get Driver

What it means

getJdbcDriverInstance() loads the JDBC driver class returned by getDriverClassName() reflectively; if instantiation or loading fails (Instantiation/IllegalAccess/ClassNotFound), it wraps the failure in NoDriverFoundException('Could not get Driver').

Solutions

  1. Add the JDBC driver artifact for your database (e.g. org.postgresql:postgresql, com.mysql:mysql-connector-j) to the test classpath
  2. Verify the driver class name matches the dependency version
  3. Run dependency:tree to confirm the driver resolves in the executing module

Example fix

// before
new JdbcDatabaseContainer(...); // no driver on classpath
// after (Gradle)
testImplementation 'org.testcontainers:postgresql'
testImplementation 'org.postgresql:postgresql:42.7.3'
Defensive patterns

Strategy: validation

Validate before calling

try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { throw new IllegalStateException("JDBC driver not on classpath"); }

Try / catch

try { conn = container.createConnection("?"); } catch (NoDriverFoundException e) { /* add the JDBC driver dependency */ throw e; }

Prevention

When it happens

Trigger: Calling createConnection(...) when the JDBC driver named by getDriverClassName() (e.g. org.postgresql.Driver) is not on the classpath, or cannot be instantiated (abstract class, no no-arg constructor, private).

Common situations: Forgetting to add the JDBC driver dependency alongside the testcontainers module; driver only present in a different scope/module; shading that strips the driver class; a driver class name mismatch after switching database types.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of testcontainers/testcontainers-java@8e549514e3 (2026-09-12). Data as JSON: /api/errors/eec91d179a382127. Report an issue: GitHub.

Appendix: source

Thrown at modules/jdbc/src/main/java/org/testcontainers/containers/JdbcDatabaseContainer.java:235

    @Override
    protected void containerIsStarted(InspectContainerResponse containerInfo) {
        logger().info("Container is started (JDBC URL: {})", this.getJdbcUrl());
        runInitScriptIfRequired();
    }

    /**
     * Obtain an instance of the correct JDBC driver for this particular database container type
     *
     * @return a JDBC Driver
     */
    public Driver getJdbcDriverInstance() throws NoDriverFoundException {
        synchronized (DRIVER_LOAD_MUTEX) {
            if (driver == null) {
                try {
                    driver = (Driver) Class.forName(this.getDriverClassName()).newInstance();
                } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                    throw new NoDriverFoundException("Could not get Driver", e);
                }
            }
        }

        return driver;
    }

    /**
     * Creates a connection to the underlying containerized database instance.
     *
     * @param queryString query string parameters that should be appended to the JDBC connection URL.
     *                    The '?' character must be included
     * @return a Connection
     * @throws SQLException if there is a repeated failure to create the connection
     */
    public Connection createConnection(String queryString) throws SQLException, NoDriverFoundException {
        return createConnection(queryString, new Properties());
    }

View on GitHub (pinned to 8e549514e3)