在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);