【问题标题】:Typesafe Config: Overriding values for unit testing purposesTypesafe Config:用于单元测试目的的覆盖值
【发布时间】:2020-06-08 23:59:25
【问题描述】:

在需要config: Config 的类的单元测试中,我想以可视方式(而不是在位于其他地方的配置文件中)声明测试的假定配置设置。

例如,我想做这样的事情:

class myClassSpec extends AnyFlatSpec{
  val myTestingConfigForThisTestCase = 3L
  val config = ConfigFactory.load()
                .withValue("my-config-path", myTestingConfigForThisTestCase)
  ...
}

但是,withValue 需要 ConfigValue,而且基本类型之间似乎没有隐式转换。

关于简单解决方案的任何想法?

【问题讨论】:

    标签: scala unit-testing typesafe-config


    【解决方案1】:

    您可能想使用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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多