gpt4 book ai didi

scala - Mockito scala 验证测试不起作用( Play Framework )

转载 作者:行者123 更新时间:2023-12-04 08:35:37 26 4
gpt4 key购买 nike

我正在尝试使用 Scala 中的 Play 框架进行单元测试。
我编写了一个类来检查配置是否正确(我有更多的错误处理,但我现在实际上使用此代码进行测试):

class TaskQueueConfig(conf: Configuration) {
val schedulingEnabled = conf.getBoolean("schedulingEnabled").get
val processingEnabled = conf.getBoolean("processingEnabled").get
val queueName = conf.getString("queue").get
}

我正在使用 play 2.1.1 的默认测试设置进行测试:
class ConfigTestSpec extends Specification with Mockito with CalledMatchers {
"TaskQueueConfig" should {
"verify calls" in {
val tqConf = mock[Configuration]
tqConf.getString("queue") returns Some("queueName")
tqConf.getBoolean("schedulingEnabled") returns Some(true)
tqConf.getBoolean("processingEnabled") returns Some(true)
Logger.error("setup done")

val config = new TaskQueueConfig(tqConf)

there was one(tqConf).getString("queue")
there were two(tqConf).getBoolean(any[String])
there were one(tqConf).getBoolean("schedulingEnabled")
there were one(tqConf).getBoolean("processingEnabled")
}
}
}

我收到以下错误:
[error] x verify calls
[error] The mock was not called as expected:
[error] configuration.getString$default$2();
[error] Wanted 1 time:
[error] -> at config.ConfigTestSpec$$anonfun$2$$anonfun$apply$4$$anonfun$apply$12.apply(ConfigTestSpec.scala:61)
[error] But was 2 times. Undesired invocation:
[error] -> at config.TaskQueueConfig.<init>(TaskQueueConfig.scala:10) (ConfigTestSpec.scala:61)

这很奇怪,因为代码非常孤立,而且 TaskQueueConfig 中显然只有 1 个 conf.getString 调用。第 10 行是带有 getString 方法的行。第 61 行是“there was one(tQConf).getString”的那一行

我该如何解决这个问题?

(曾经和曾经之间没有区别)。

PS:我知道这个例子测试没什么用,但我有更复杂的配置,其中有一些规则需要测试。

更新 1
getString 方法有两个参数,第二个参数的默认值是 None(它的类型是 Option[Set[String]])。当我在设置和验证中明确添加 None 时,它​​仍然不起作用。但是当我添加 null相反,我让它工作。
  val tqConf = mock[Configuration]

tqConf.getString("queue", null) returns Some("queueName")
tqConf.getBoolean("schedulingEnabled") returns Some(true)
tqConf.getBoolean("processingEnabled") returns Some(true)

val c = new TaskQueueConfig(tqConf)

there was one(tqConf).getString("queue", null)
there was one(tqConf).getString(any[String], any[Option[Set[String]]])
there were two(tqConf).getBoolean(any[String])
there was one(tqConf).getBoolean("schedulingEnabled")
there was one(tqConf).getBoolean("processingEnabled")

c.processingEnabled must beTrue
c.schedulingEnabled must beTrue
c.queueName must be("queueName")

所以我想现在的问题是,为什么我必须使用 null?

最佳答案

您必须使用 null在两个参数getString()调用,因为 Mockito 要求您对所有参数使用匹配器或根本不使用匹配器。

所以混"queue" (文字)与 any[Option[Set[String]]] (匹配器)不起作用。有时 Mockito 可以计算出你已经这样做了,它会给你一个错误,但似乎你在这里不走运......

尝试在所有位置使用匹配器,如下所示:

tqConf.getString(org.mockito.Matchers.eq("queue"), any[Option[Set[String]]]) returns Some("queueName")

并验证:
there was one(tqConf).getString(eq("queue"), any[Option[Set[String]]])

关于scala - Mockito scala 验证测试不起作用( Play Framework ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18637755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com