找不到路径 PostgreSQLContainer testContainers

Unable to find the path PostgreSQLContainer testContainers

在 Postgres 版本中使用 Test Containers 时,我无法找到我的资源映射。 我正在尝试类似的东西:

     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();
}

但是,我找不到该文件。我什至尝试将脚本 create.sh 包含在同一目录中,但找不到:

java.lang.IllegalArgumentException:找不到路径为 ../../../../../../../create.sh 的资源这些类加载器中的任何一个

项目结构

有人遇到同样的问题吗?

withClasspathResourceMapping 的用法是当您设置了类路径时。就我而言,我没有,而且这种方法不起作用。作为替换,我尝试使用 addFileSystemBind 并且工作得很好:

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

withClasspathResourceMapping 使用 ClassLoader#getResource。 该参数与您的 class 无关。相反,它使用当前 class 路径。 在您的情况下,它应该适用于:

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