Populate a database with TestContainers in a SpringBoot integration test

前端 未结 6 1865
自闭症患者
自闭症患者 2021-02-20 07:10

I am testing TestContainers and I would like to know how to populate a database executing a .sql file to create the structure and add some rows.

How to do it?

         


        
6条回答
  •  广开言路
    2021-02-20 07:58

    There is one more option, if you are defining Postgres container manually without fancy testcontainers JDBC url stuff, not related to Spring directly. Postgres image allows to link directory containing sql scripts to container volume and auto-executes them.

    GenericContainer pgDb = new PostgreSQLContainer("postgres:9.4-alpine")
      .withFileSystemBind("migrations/sqls", "/docker-entrypoint-initdb.d",
        BindMode.READ_ONLY)
    

    Also if you need something in runtime, you can always do pgDb.execInContainer("psql ....").

提交回复
热议问题