简体   繁体   中英

How to connect multiple database with testcontainers

I have a app using an utility library where classes are defined as

@Entity
@Table(name = "event" ,  catalog = "GE_events")
public class Event{
...
}

while the app domain classes belong to the default catalog such as:

@Entity
@Table(name = "user")
public class User{
...
}

Application services use both and this isn't a problem as far as the schema has already been created (either manually or with liquibase).

I wish to write an integration test using testcontainers

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, 
  properties = {"spring.jpa.hibernate.ddl-auto=create",
    "spring.jpa.show_sql=true",
    "javax.persistence.sql-load-script-source=data.sql",
    "spring.datasource.url= jdbc:tc:mariadb://localhost/test",
    "spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver"})
@RunWith(SpringRunner.class)
public class ServiceIntTest {
...
}

in this way the table GE_events.event is never created (and obviously the test crashes badly).

How can I achieve this result?

You can use Liquibase to create the schema during test execution.

While you can still use the TC JDBC URL for this (I'd recommend using daemon mode, ?TC_DAEMON=true in this case, to make sure no new database containers are created), most users tend to use the object API in this case, so creating a MariaDBContainer object and wire it up using Spring's DynamicPropertySource .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM