src
main
java
config
DataSourceProperties.java
resources
application.yml
test
kotlin
service
AuthService.kt
@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文件中的属性。也许我想念另一个配置,也许我对注释不对,或者它可能取决于configure,yml和测试文件在结构中的位置。请给我一个提示。
尝试设置,例如应该帮助 - @TestPropertySource(properties = ["spring.mail.properties.mail.smtp.auth: false",
"spring.mail.properties.mail.smtp.starttls.enable: false"])