在 Spring Boot 单元测试中,如何模拟 @ConstructorBinding @ConfigurationProperties 数据类?
我想用不同的配置来测试 FtpService(一个
@Service
,其中有一个 RestTemplate
)。
FtpService 的属性来自 Kotlin 数据类 - UrlProperties - 它用
ConstructorBinding
和 @ConfigurationProperties
进行注释。
注意:FtpService 的构造函数从 UrlProperties 中提取属性。这意味着 UrlProperties 必须在 Spring 加载 FtpService 之前被模拟 和 被存根 错误
Cannot bind @ConfigurationProperties for bean 'urlProperties'. Ensure that @ConstructorBinding has not been applied to regular bean
import com.example.service.FtpService
import com.example.service.UrlProperties
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.context.annotation.Bean
import org.springframework.test.context.ContextConfiguration
@TestConfiguration
@SpringBootTest(classes = [FtpService::class])
@AutoConfigureWebClient(registerRestTemplate = true)
class FtpServiceTest
@Autowired constructor(
private val ftpService: FtpService
) {
// MockBean inserted into Spring Context too late,
// FtpService constructor throws NPE
// @MockBean
// lateinit var urlProperties: UrlProperties
@ContextConfiguration
class MyTestContext {
// error -
// > Cannot bind @ConfigurationProperties for bean 'urlProperties'.
// > Ensure that @ConstructorBinding has not been applied to regular bean
var urlProperties: UrlProperties = mock(UrlProperties::class.java)
@Bean
fun urlProperties() = urlProperties
// error -
// > Cannot bind @ConfigurationProperties for bean 'urlProperties'.
// > Ensure that @ConstructorBinding has not been applied to regular bean
// @Bean
// fun urlProperties(): UrlProperties {
// return UrlProperties(
// UrlProperties.FtpProperties(
// url = "ftp://localhost:21"
// ))
// }
}
@Test
fun `test fetch file root`() {
`when`(MyTestContext().urlProperties.ftp)
.thenReturn(UrlProperties.FtpProperties(
url = "ftp://localhost:21"
))
assertEquals("I'm fetching a file from ftp://localhost:21!",
ftpService.fetchFile())
}
@Test
fun `test fetch file folder`() {
`when`(MyTestContext().urlProperties.ftp)
.thenReturn(UrlProperties.FtpProperties(
url = "ftp://localhost:21/user/folder"
))
assertEquals("I'm fetching a file from ftp://localhost:21/user/folder!",
ftpService.fetchFile())
}
}
解决方法 - 手动定义每个测试
import com.example.service.FtpService
import com.example.service.UrlProperties
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.springframework.boot.web.client.RestTemplateBuilder
class FtpServiceTest2 {
private val restTemplate =
RestTemplateBuilder()
.build()
private lateinit var ftpService: FtpService
private lateinit var urlProperties: UrlProperties
@BeforeEach
fun beforeEachTest() {
urlProperties = mock(UrlProperties::class.java)
`when`(urlProperties.ftp)
.thenReturn(UrlProperties.FtpProperties(
url = "default"
))
ftpService = FtpService(restTemplate, urlProperties)
}
/** this is the only test that allows me to redefine 'url' */
@Test
fun `test fetch file folder - redefine`() {
urlProperties = mock(UrlProperties::class.java)
`when`(urlProperties.ftp)
.thenReturn(UrlProperties.FtpProperties(
url = "ftp://localhost:21/redefine"
))
// redefine the service
ftpService = FtpService(restTemplate, urlProperties)
assertEquals("I'm fetching a file from ftp://localhost:21/redefine!",
ftpService.fetchFile())
}
@Test
fun `test default`() {
assertEquals("I'm fetching a file from default!",
ftpService.fetchFile())
}
@Test
fun `test fetch file root`() {
`when`(urlProperties.ftp)
.thenReturn(UrlProperties.FtpProperties(
url = "ftp://localhost:21"
))
assertEquals("I'm fetching a file from ftp://localhost:21!",
ftpService.fetchFile())
}
@Test
fun `test fetch file folder`() {
doReturn(
UrlProperties.FtpProperties(
url = "ftp://localhost:21/user/folder"
)).`when`(urlProperties).ftp
assertEquals("I'm fetching a file from ftp://localhost:21/user/folder!",
ftpService.fetchFile())
}
@Test
fun `test fetch file folder - reset`() {
Mockito.reset(urlProperties)
`when`(urlProperties.ftp)
.thenReturn(UrlProperties.FtpProperties(
url = "ftp://localhost:21/mockito/reset/when"
))
assertEquals("I'm fetching a file from ftp://localhost:21/mockito/reset/when!",
ftpService.fetchFile())
}
@Test
fun `test fetch file folder - reset & doReturn`() {
Mockito.reset(urlProperties)
doReturn(
UrlProperties.FtpProperties(
url = "ftp://localhost:21/reset/doReturn"
)).`when`(urlProperties).ftp
assertEquals("I'm fetching a file from ftp://localhost:21/reset/doReturn!",
ftpService.fetchFile())
}
}
package com.example
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.runApplication
@SpringBootApplication
@EnableConfigurationProperties
@ConfigurationPropertiesScan
class MyApp
fun main(args: Array<String>) {
runApplication<MyApp>(*args)
}
package com.example.service
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
@Service
class FtpService(
val restTemplate: RestTemplate,
urlProperties: UrlProperties,
val ftpProperties: UrlProperties.FtpProperties = urlProperties.ftp
) {
fun fetchFile(): String {
println(restTemplate)
return "I'm fetching a file from ${ftpProperties.url}!"
}
}
package com.example.service
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding
@ConstructorBinding
@ConfigurationProperties("url")
data class UrlProperties(val ftp: FtpProperties) {
data class FtpProperties(
val url: String,
)
}
@TestPropertySource
注释提供自定义配置并提供不同的配置。通过在
@SpringBootTest
注释中提供属性可以实现类似的结果。那么您将不会创建自定义 URL 属性 bean。另请参阅:https://www.baeldung.com/spring-tests-override-properties这种方法可能会很昂贵,因为将为每个具有此注释的测试类以及以下测试重新构建 Spring 上下文,这可能会为您的整体测试运行时间增加几秒。
选项2
@MockkBean
(来自
https://github.com/Ninja-Squad/springmockk)模拟您的配置bean并指定返回值。这与您的解决方法不同,因为您仍然拥有完整的 Spring 上下文和只有一个模拟。 示例:
@TestConfiguration
@SpringBootTest(classes = [FtpService::class])
@AutoConfigureWebClient(registerRestTemplate = true)
class FtpServiceTest {
@Autowired
private lateinit var ftpService: FtpService
@MockkBean
private lateinit var urlProperties: UrlProperties
@Nested
inner class `scenario 1` {
@BeforeEach
fun setup() {
every { urlProperties.ftp } returns "url for scenario 1"
}
}
@Nested
inner class `scenario 2` {
@BeforeEach
fun setup() {
every { urlProperties.ftp } returns "url for scenario 2"
}
}
}
在 Spring 中使用模拟也会对性能产生影响,而这个影响会比选项 1 中的小。
选项3
示例:
@TestConfiguration
@SpringBootTest(classes = [FtpService::class])
@AutoConfigureWebClient(registerRestTemplate = true)
class FtpServiceTest {
// example for a bean from the context you want to inject but not define yourself
@Autowired
private lateinit var otherBean: OtherBean
@Nested
inner class `scenario 1` {
private val urlProperties = UrlProperties(ftp = "url for scenario 1")
private val ftpService = FtpService(urlProperties, otherBean)
}
@Nested
inner class `scenario 2` {
private val urlProperties = UrlProperties(ftp = "url for scenario 2")
private val ftpService = FtpService(urlProperties, otherBean)
}
}
这将是最快的方法 - 结合 Spring 测试切片,您可能会加快速度。但它不会测试实际的有线服务。
替代方案