我有一个看起来像这样的配置获取器。
def getForCountry[A](path: String, fallbackToDefault: Boolean)
(implicit loader: ConfigLoader[A], ac: AppContext): A = {
configuration.getOptional[A](s"${ac.country}.$path") match {
case Some(value) =>
value
case None if fallbackToDefault =>
configuration.get[A](path)
case None if !fallbackToDefault =>
throw new RuntimeException(s"${ac.country}.$path key not found in configuration")
}
相同方法的调用如下——
val countrySpecificConfig =
configurationHelper.getForCountry[Map[String, String]]("googleCloudPlatform.jobConfig.demandBasedPricing", fallbackToDefault = false)
现在我想在我的单元测试中模拟 getForCountry 方法 -
when(configurationHelper
.getForCountry[Map[String, String]]("googleCloudPlatform.jobConfig.demandBasedPricing", fallbackToDefault = false))
.thenReturn(countryPricingWeekConfiguation)
令人惊讶的是,这种期望似乎没有正确设置。在执行测试时,模拟返回 null。
有关如何进行此操作的任何线索?如果您需要更多详细信息,请随时告诉我。