diff --git a/README.adoc b/README.adoc index 897636f..54a4d90 100644 --- a/README.adoc +++ b/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 diff --git a/src/main/kotlin/com/example/blog/BlogApplication.kt b/src/main/kotlin/com/example/blog/BlogApplication.kt index e9f7593..fba625f 100644 --- a/src/main/kotlin/com/example/blog/BlogApplication.kt +++ b/src/main/kotlin/com/example/blog/BlogApplication.kt @@ -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) { diff --git a/src/test/kotlin/com/example/blog/HttpControllersTests.kt b/src/test/kotlin/com/example/blog/HttpControllersTests.kt index 4c5b4e7..c304b46 100644 --- a/src/test/kotlin/com/example/blog/HttpControllersTests.kt +++ b/src/test/kotlin/com/example/blog/HttpControllersTests.kt @@ -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