Leverage @ConfigurationProperties scanning
This commit is contained in:
26
README.adoc
26
README.adoc
@@ -895,18 +895,6 @@ data class BlogProperties(var title: String, val banner: Banner) {
|
||||
}
|
||||
----
|
||||
|
||||
Then we enable it at `BlogApplication` level.
|
||||
|
||||
`src/main/kotlin/com/example/blog/BlogApplication.kt`
|
||||
[source,kotlin]
|
||||
----
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(BlogProperties::class)
|
||||
class BlogApplication {
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
To generate https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#configuration-metadata-annotation-processor[your own metadata] in order to get these custom properties recognized by your IDE, https://kotlinlang.org/docs/reference/kapt.html[kapt should be configured] with the `spring-boot-configuration-processor` dependency as following.
|
||||
|
||||
`build.gradle.kts`
|
||||
@@ -986,6 +974,20 @@ class HtmlController(private val repository: ArticleRepository,
|
||||
// ...
|
||||
----
|
||||
|
||||
Since `@ConfigurationProperties` are not scanned automatically in test slices, we need to configure it explicitly in our
|
||||
test annotated with `@WebMvcTest`.
|
||||
|
||||
`src/main/kotlin/com/example/blog/BlogApplication.kt`
|
||||
[source,kotlin]
|
||||
----
|
||||
@WebMvcTest
|
||||
@EnableConfigurationProperties(BlogProperties::class)
|
||||
class HttpControllersTests(@Autowired val mockMvc: MockMvc) {
|
||||
// ...
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
Restart the web application, refresh `http://localhost:8080/`, you should see the banner on the blog homepage.
|
||||
|
||||
== Conclusion
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.example.blog
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.boot.runApplication
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(BlogProperties::class)
|
||||
class BlogApplication
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.ninjasquad.springmockk.MockkBean
|
||||
import io.mockk.every
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
||||
@@ -11,6 +12,7 @@ import org.springframework.http.MediaType
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
|
||||
|
||||
@WebMvcTest
|
||||
@EnableConfigurationProperties(BlogProperties::class)
|
||||
class HttpControllersTests(@Autowired val mockMvc: MockMvc) {
|
||||
|
||||
@MockkBean
|
||||
|
||||
Reference in New Issue
Block a user