无法找到路径 PostgreSQLContainer testContainers。

4

在使用测试容器时,我无法找到我的资源映射,在Postgres版本中。我正在尝试类似于以下内容:

     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的资源在任何类加载器中都无法找到
项目结构

enter image description here

有同样问题的人吗?

2个回答

2

withClasspathResourceMapping 的使用是在你已经设置了类路径的情况下。在我的情况下,我没有设置,所以这个方法不起作用。作为替代,我尝试了 addFileSystemBind,并且正常工作:

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

2

withClasspathResourceMapping 使用 ClassLoader#getResource 方法。 该参数与您的类无关,而是使用当前类路径。 在您的情况下,应该使用以下内容:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接