[英]Typesafe Config: Overriding values for unit testing purposes
在需要config: Config
的 class 的單元測試中,我想以可視方式(而不是在位於其他地方的配置文件中)聲明測試的假定配置設置。
例如,我想做這樣的事情:
class myClassSpec extends AnyFlatSpec{
val myTestingConfigForThisTestCase = 3L
val config = ConfigFactory.load()
.withValue("my-config-path", myTestingConfigForThisTestCase)
...
}
但是, withValue
需要一個ConfigValue
,並且基本類型之間似乎沒有隱式轉換。
關於簡單解決方案的任何想法?
您可能想使用ConfigValueFactory
- 很可能是這樣的
ConfigFactory.load()
.withValue(
"my-config-path",
ConfigValueFactory.fromAnyRef(myTestingConfigForThisTestCase)
)
雖然這不能很好地擴展 - 即,如果您需要覆蓋超過 2-3 個設置,它會比ConfigFactory.parseString
+ withFallback
獲得更多的樣板:
val configOverride = """
{
my-config-path: $myTestingConfigForThisTestCase
other-config {
...
}
}
"""
val config = ConfigFactory.parseString(configOverride)
.withFallback(ConfigFactory.load())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.