joe*_*uch 3 unit-testing scala typesafe-config
在需要一个类的单元测试中config: Config,我想以可视方式声明(而不是在位于其他位置的配置文件中)测试的假定配置设置。
例如,我想做这样的事情:
class myClassSpec extends AnyFlatSpec{
val myTestingConfigForThisTestCase = 3L
val config = ConfigFactory.load()
.withValue("my-config-path", myTestingConfigForThisTestCase)
...
}
Run Code Online (Sandbox Code Playgroud)
然而,withValue预计ConfigValue并且基本类型和 that 之间似乎没有隐式转换。
关于简单的解决方案有什么想法吗?
你可能想使用ConfigValueFactory- 最有可能的东西
ConfigFactory.load()
.withValue(
"my-config-path",
ConfigValueFactory.fromAnyRef(myTestingConfigForThisTestCase)
)
Run Code Online (Sandbox Code Playgroud)
但这不能很好地扩展 - 也就是说,如果您需要覆盖超过 2-3 个设置,它会比ConfigFactory.parseString+更多的样板代码withFallback:
val configOverride = """
{
my-config-path: $myTestingConfigForThisTestCase
other-config {
...
}
}
"""
val config = ConfigFactory.parseString(configOverride)
.withFallback(ConfigFactory.load())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2963 次 |
| 最近记录: |