加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Mockito scala验证测试不起作用(播放框架)

发布时间:2020-12-16 08:54:45 所属栏目:安全 来源:网络整理
导读:我正在尝试使用 Scala中的Play框架进行单元测试. 我写了一个类来检查配置是否正确(我有更多的错误处理,但我实际上现在使用此代码进行测试): class TaskQueueConfig(conf: Configuration) { val schedulingEnabled = conf.getBoolean("schedulingEnabled").g
我正在尝试使用 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行是“有一个(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?

解决方法

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

因此将“队列”(文字)与任何[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]]])

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读