[英]Spring Boot properties in 'application.yml' not loading from JUnit Test
[英]How to read variable from application.yml with spring boot and use it in kotlin test
src
main
java
config
DataSourceProperties.java
resources
application.yml
test
kotlin
service
AuthService.kt
我有java configure類
@Configuration @ConfigurationProperties(prefix = "datasource") class DataSourceProperties { private String apiUrl; public String getApiUrl() { return apiUrl; } public void setUrl(String apiUrl) { this.apiUrl = apiUrl; }
我有科特林服務班
@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文件中的屬性。 也許我錯過了另一個配置,也許我對注解不正確,或者它取決於配置文件,yml和測試文件在結構中的位置。 請給我一個提示。
嘗試簡單地設置,例如應該提供幫助- @TestPropertySource(properties = ["spring.mail.properties.mail.smtp.auth: false", "spring.mail.properties.mail.smtp.starttls.enable: false"])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.