如何使用spring boot从application.yml读取变量并在kotlin测试中使用它

问题描述 投票:1回答:1
src
  main
    java
       config
          DataSourceProperties.java
    resources
      application.yml
  test
    kotlin
      service
          AuthService.kt
  1. 我有Image of application.yml file application.yml
  2. 我有java配置类 @Configuration @ConfigurationProperties(prefix = "datasource") class DataSourceProperties { private String apiUrl; public String getApiUrl() { return apiUrl; } public void setUrl(String apiUrl) { this.apiUrl = apiUrl; }
  3. 我有服务kotlin类 @RunWith(SpringRunner::class) @SpringBootApplication @Profile("dev") class AuthService { @Value("\${datasource.apiUrl}") lateinit var apiUrl: String fun registerUser(user: RequestUser): ErrorResponse { return given() .log().all() .body(user) .contentType(ContentType.JSON) .`when`().post("$apiUrl/security") .then().statusCode(StatusCodes.BAD_REQUEST) .extract() .`as`(ErrorResponse::class.java) } }

当我在测试中执行请求时,我得到了

kotlin.UninitializedPropertyAccessException:lateinit属性apiUrl尚未初始化

这就是我有疑问的原因?如何在测试类中使用yml文件中的属性。也许我想念另一个配置,也许我对注释不对,或者它可能取决于configure,yml和测试文件在结构中的位置。请给我一个提示。

java spring-boot kotlin yaml
1个回答
0
投票

尝试设置,例如应该帮助 - @TestPropertySource(properties = ["spring.mail.properties.mail.smtp.auth: false", "spring.mail.properties.mail.smtp.starttls.enable: false"])

© www.soinside.com 2019 - 2024. All rights reserved.