找不到路径PostgreSQLContainer testContainers

时间:2018-11-26 12:16:22

标签: java postgresql testcontainers

在Postgres版本中使用JMeter – Logging Into Salesforce for Automated Testing时,我找不到资源映射。 我正在尝试类似的东西:

     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的资源

项目结构

Test Containers

有人遇到同样的问题吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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