简体   繁体   English

Typesafe Config:用于单元测试目的的覆盖值

[英]Typesafe Config: Overriding values for unit testing purposes

In a unit test of a class that requires a config: Config , I'd like to declare visually (not in a config file located in another place) the assumed configurations settings for the test.在需要config: Config的 class 的单元测试中,我想以可视方式(而不是在位于其他地方的配置文件中)声明测试的假定配置设置。

So for example, I'd like to do something like this:例如,我想做这样的事情:

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

However, withValue expects a ConfigValue and there seem to be no implicit conversions between basic types and that.但是, withValue需要一个ConfigValue ,并且基本类型之间似乎没有隐式转换。

Any ideas on a simple solution?关于简单解决方案的任何想法?

You might want to use ConfigValueFactory - most likely something like您可能想使用ConfigValueFactory - 很可能是这样的

ConfigFactory.load()
  .withValue(
    "my-config-path", 
    ConfigValueFactory.fromAnyRef(myTestingConfigForThisTestCase)
  )

This doesn't scale well though - ie if you need overriding more than 2-3 settings it gets more boilerplaty than ConfigFactory.parseString + withFallback :虽然这不能很好地扩展 - 即,如果您需要覆盖超过 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM