为什么Testcontainers 找不到用init 脚本创建的表?

问题描述

我尝试在启动期间访问由初始化脚本创建的 PostgresqlContainer 中的表,但 Testcontainers 说他找不到该表。为什么???

@Entity
@Table(name = "named_person")
@Data
public class NamedPerson {

    private static final long serialVersionUID = 8799954029578723024L;

    @Column(name = "xxx")
    private String xxx;
}

测试的基类:

@ActiveProfiles("test")
@Testcontainers
@SpringBoottest(webEnvironment = SpringBoottest.WebEnvironment.RANDOM_PORT)
public abstract class AbstractApplicationIT {

    final static DockerImageName POSTGRES_IMAGE = DockerImageName.parse("postgres:13.2-alpine");

    @Container
    public static PostgresqlContainer<?> postgresqlContainer = new PostgresqlContainer<>(POSTGRES_IMAGE);

    @Test
    public void contextLoads() {
    }

}

我的测试班:

@Transactional
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class NamedPersonIT extends AbstractApplicationIT {

    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.dbname}")
    private String dbname;
    @Value("${spring.datasource.initScript}")
    private String initScript;

    @Autowired
    private NamedPersonRepository npr;

    @BeforeAll
    public void setup() {
        postgresqlContainer = new PostgresqlContainer<>(POSTGRES_IMAGE)
                .withDatabaseName(dbname)
                .withUsername(username)
                .withPassword(password)
                .withInitScript(initScript);
        postgresqlContainer.start();
    }

    @Test
    public void checkDbContainerIsAlive() {
        assertthat(this.npr.findAll()).isNotNull();
    }
}

错误信息:

Caused by: org.h2.jdbc.JdbcsqlSyntaxErrorException: Tabelle "PERSON" nicht gefunden
Table "PERSON" not found; sql statement:
select namedpe0_.id as id1_2_,namedpe0_.xxx as xxx2_2_ from person namedpe0_ [42102-200]

初始化脚本:

DROP TABLE IF EXISTS named_person;
CREATE TABLE named_person (
  id                VARCHAR(36) PRIMARY KEY,xxx               VARCHAR(12)
);
INSERT INTO person VALUES ('1','abcdefg');

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)