简体   繁体   English

如何从 Testcontainer 的应用程序文件加载属性?

[英]How do I load properties from application file for Testcontainer?

Currently, I am using Testcontainer to implement integration testing for database in spring boot.目前,我正在使用 Testcontainer 在 spring 引导中实现数据库的集成测试。 How do I init the Testcontainer with the application.yml.如何使用 application.yml 初始化 Testcontainer。 I don't want to use @DynamicPropertySource.我不想使用@DynamicPropertySource。

public static PostgreSQLContainer container = new PostgreSQLContainer("postgres:12")
spring:
  datasource:
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/db
    username: user
    password: password

You can simply add tc to you spring.datasource.url property.您可以简单地将 tc 添加到您的 spring.datasource.url 属性。

spring:
  datasource:
    driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
    url: jdbc:tc:postgresql:12:///db?TC_TMPFS=/testtmpfs:rw
    username: user
    password: password

I prefer not to use application.yaml just create application-test.yaml and override default one.我不想使用 application.yaml 只是创建 application-test.yaml 并覆盖默认值。 You can check https://www.testcontainers.org/modules/databases/jdbc/ .您可以查看https://www.testcontainers.org/modules/databases/jdbc/

Also you can override with @testpropertysource annotation.您也可以使用 @testpropertysource 注释覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将属性动态加载到应用程序测试容器? - How to load properties dynamically to application testcontainer? 如何从数据库而不是Spring应用程序中的属性文件加载属性 - How to load properties from database instead of properties file in spring application 如何从属性文件中指定的XML文件加载Spring bean定义? - How do I load Spring bean definitions from XML file specified in a properties file? 如何使用Adobe Flex从客户端加载.properties文件? - How do I load a .properties file from a client using Adobe Flex? 如何使用Spring将嵌套键值对从属性文件加载到Java对象中? - How do I load nested key value pairs from a properties file into a Java object using Spring? 如何使用JUnit / Ant加载属性文件? - How do I load a properties file with JUnit / Ant? testcontainer 启动后,如何在集成测试中覆盖 application.properties 中定义的端口? - How to overwrite ports defined in application.properties in integration tests after testcontainer started? 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? 如何从XML属性文件加载变量? - How can I load variables from an XML properties file? 如何从应用程序访问MSI公共属性? - How do I access MSI public properties from my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM