如何使用已在Spring Boot YML文件中配置的TestContainer工作

发布于 2025-01-20 09:45:55 字数 815 浏览 0 评论 0 原文

我正在尝试使用TestContainers MSSQLServerContainer,但我找不到任何信息如何获取在YML文件中配置的实例MSSQLServerContainer。 我在YML文件中具有类似的配置

site-db:
    url: jdbc:tc:sqlserver://localhost:1111;DatabaseName=nameDb;sendStringParametersAsUnicode=false?TC_INITSCRIPT=sql/create_tables.sql
    jdbc-url: jdbc:tc:sqlserver://localhost:1111;DatabaseName=nameDb;sendStringParametersAsUnicode=false?TC_INITSCRIPT=sql/create_tables.sql
    driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
    jndiName: java:comp/env/jdbc/siteDb
    username: username
    password: password 

在运行测试时,

,然后容器也启动,但是我可以获得此容器的实例。如果我尝试编写这篇文章:

 @Rule
    private MSSQLServerContainer mssqlServerContainer = new MSSQLServerContainer();

那么我会得到新容器

可以从YML文件中配置的TestContainer获得连接的方法吗?

I am trying to work with testcontainers MSSQLServerContainer but I can't find any information how to get instance MSSQLServerContainer which was configured in yml file.
I have configuration in yml file something like this

site-db:
    url: jdbc:tc:sqlserver://localhost:1111;DatabaseName=nameDb;sendStringParametersAsUnicode=false?TC_INITSCRIPT=sql/create_tables.sql
    jdbc-url: jdbc:tc:sqlserver://localhost:1111;DatabaseName=nameDb;sendStringParametersAsUnicode=false?TC_INITSCRIPT=sql/create_tables.sql
    driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
    jndiName: java:comp/env/jdbc/siteDb
    username: username
    password: password 

When I run my test then the container starts too but I can get instance of this container.

if I try write this:

 @Rule
    private MSSQLServerContainer mssqlServerContainer = new MSSQLServerContainer();

then I get new container

Is there some way to get connection from testcontainer configured in yml file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2025-01-27 09:45:55

当您自己将容器实例化为这样的字段时:

@Rule
private MSSQLServerContainer mssqlServerContainer = new MSSQLServerContainer();

您需要使用 mssqlservercontainer.getjdbcurl()使用 @dynamicpropertysource

但是,因为您正在使用TestContContainers JDBC驱动程序 jdbc:tc:sqlserver://localhost:1111;DatabaseName=nameDb;sendStringParametersAsUnicode=false?TC_INITSCRIPT=sql/create_tables.sql, this is not necessary, since connecting to the JDBC-URL will automatically spawn a container.

如果您需要对容器的程序化访问,我建议您改用 @dynamicpropertysource 方法。

When you are instantiating your container yourself as a field like this:

@Rule
private MSSQLServerContainer mssqlServerContainer = new MSSQLServerContainer();

you need to inject the mssqlServerContainer.getJdbcUrl() value into Spring using @DynamicPropertySource:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/DynamicPropertySource.html

However, since you are using the Testcontainers JDBC Driver Proxy approach by specifying jdbc:tc:sqlserver://localhost:1111;DatabaseName=nameDb;sendStringParametersAsUnicode=false?TC_INITSCRIPT=sql/create_tables.sql, this is not necessary, since connecting to the JDBC-URL will automatically spawn a container.

If you need programmatic access to the container, I'd recommend to use the @DynamicPropertySource approach instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文