I try to access a table in a PostgreSQLContainer created by an init script during startup but Testcontainers says that he can't find the table. Why???
@Entity
@Table(name = "named_person")
@Data
public class NamedPerson {
private static final long serialVersionUID = 8799954029578723024L;
@Column(name = "xxx")
private String xxx;
}
The test's base class:
@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() {
}
}
My test class:
@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();
}
}
The error message:
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]
The initScript:
DROP TABLE IF EXISTS named_person;
CREATE TABLE named_person (
id VARCHAR(36) PRIMARY KEY,
xxx VARCHAR(12)
);
INSERT INTO person VALUES ('1', 'abcdefg');