testcontainers/testcontainers-java · error · org.testcontainers.ext.ScriptUtils.ScriptLoadException

Could not load classpath init script

Error message

Could not load classpath init script: ${initScriptPath}. Resource not found.

What it means

CassandraContainer.runInitScriptIfRequired loads the configured init script (via withInitScript) from the thread context classpath. If getResource returns null, the resource does not exist on the classpath and a ScriptLoadException is thrown. The message includes the configured initScriptPath.

Solutions

  1. Place the script under src/test/resources (e.g. src/test/resources/init.cql) and rebuild
  2. Reference it with the correct classpath-relative path, e.g. withInitScript("cassandra/init.cql") for src/test/resources/cassandra/init.cql
  3. Verify the resource exists in the build output (target/test-classes) and is not filtered/excluded in Maven/Gradle config

Example fix

// before
new CassandraContainer("cassandra:4.0").withInitScript("/home/me/init.cql");
// after (file moved to src/test/resources/db/init.cql)
new CassandraContainer("cassandra:4.0").withInitScript("db/init.cql");
Defensive patterns

Strategy: validation

Validate before calling

URL r = Thread.currentThread().getContextClassLoader().getResource("db/init.cql");
if (r == null) throw new IllegalStateException("init script missing from test classpath");

Try / catch

try { container.start(); } catch (ContainerLaunchException e) { /* check classpath resource presence */ }

Prevention

When it happens

Trigger: container.withInitScript("init.cql").start() where init.cql is not present on the test classpath (resource lookup via context classloader returns null).

Common situations: Init script placed in project root instead of src/test/resources; wrong leading path (classpath lookup never resolves absolute filesystem paths); file excluded from the jar/test resources by build config.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at modules/cassandra/src/main/java/org/testcontainers/containers/CassandraContainer.java:102

    protected void configure() {
        optionallyMapResourceParameterAsVolume(CONTAINER_CONFIG_LOCATION, configLocation);
    }

    @Override
    protected void containerIsStarted(InspectContainerResponse containerInfo) {
        runInitScriptIfRequired();
    }

    /**
     * Load init script content and apply it to the database if initScriptPath is set
     */
    private void runInitScriptIfRequired() {
        if (initScriptPath != null) {
            try {
                URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath);
                if (resource == null) {
                    logger().warn("Could not load classpath init script: {}", initScriptPath);
                    throw new ScriptLoadException(
                        "Could not load classpath init script: " + initScriptPath + ". Resource not found."
                    );
                }
                String cql = IOUtils.toString(resource, StandardCharsets.UTF_8);
                DatabaseDelegate databaseDelegate = getDatabaseDelegate();
                ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, cql);
            } catch (IOException e) {
                logger().warn("Could not load classpath init script: {}", initScriptPath);
                throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e);
            } catch (ScriptException e) {
                logger().error("Error while executing init script: {}", initScriptPath, e);
                throw new ScriptUtils.UncategorizedScriptException(
                    "Error while executing init script: " + initScriptPath,
                    e
                );
            }
        }
    }

View on GitHub (pinned to 8e549514e3)