Julia Bel
Julia Bel

Reputation: 347

Unable to find the path PostgreSQLContainer testContainers

I am unable to find my resource map when using Test Containers, in Postgres version. I am trying something like it:

     private static PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer("postgres")
            .withDatabaseName(DATABASE_NAME)
            .withUsername(USER)
            .withPassword(PASSWORD);

    @ClassRule
    @BeforeAll
    public static void initContainer() {
        postgresqlContainer.withExposedPorts(5432);

        postgresqlContainer.withClasspathResourceMapping("../../../../../../../create.sh",
                "/docker-entrypoint-initdb.d/00_create.sh",
                BindMode.READ_ONLY);
        postgresqlContainer.start();
}

However, I can't find the file. I even tried to include the script create.sh in the same directory, but it can't be found:

java.lang.IllegalArgumentException: Resource with path ../../../../../../../create.sh could not be found on any of these classloaders

Project Structure

enter image description here

Anyone with the same issue?

Upvotes: 4

Views: 4691

Answers (2)

bsideup
bsideup

Reputation: 3063

withClasspathResourceMapping uses ClassLoader#getResource. The argument is not relative to your class. Instead, it uses current classpath. In your case, it should work with:

withClasspathResourceMapping("/db/create.sh", "/docker-entrypoint-initdb.d/00_create.sh")

Upvotes: 2

Julia Bel
Julia Bel

Reputation: 347

The usage of withClasspathResourceMapping is when you have a classpath set. In my case, I didn't have and this method doesn't work. As replacement, I tried with addFileSystemBind and worked just fine:

postgresqlContainer.addFileSystemBind(scriptsPath + File.separator + "create.sh",
                "/docker-entrypoint-initdb.d/00_create.sh",
                BindMode.READ_ONLY);

Upvotes: 2

Related Questions