【问题标题】:using jmock in Scala with type parameterized function在 Scala 中使用带有类型参数化函数的 jmock
【发布时间】:2019-03-13 14:13:33
【问题描述】:

我想在 Scala Spark 中测试一个从 RDD 写入输出的函数。

此测试的一部分是使用 jmock 在 RDD 上模拟地图

val customerRdd = mockery.mock(classOf[RDD[Customer]], "rdd1")
val transformedRddToWrite = mockery.mock(classOf[RDD[TransformedCustomer]], "rdd2")

mockery.checking(new Expectations() {{
  // ...
  oneOf(customerRdd).map(
    `with`(Expectations.any(classOf[Customer => TransformedCustomer]))
  )
  will(Expectations.returnValue(transformedRddToWrite))
  // ...
}})

但是,每当我尝试运行此测试时,都会收到以下错误: not all parameters were given explicit matchers: either all parameters must be specified by matchers or all must be specified by values, you cannot mix matchers and values,尽管我已经为.map 的所有参数指定了匹配器。

我该如何解决这个问题? jMock 能否支持对带有隐式类标签的 Scala 函数参数进行匹配?

【问题讨论】:

    标签: scala apache-spark jmock


    【解决方案1】:

    我认为 jMock 自 2012 年以来已被放弃。但如果您喜欢它,那么您将获得更多力量。问题之一是map 根据签名需要ClassTag[U]

    def map[U: ClassTag](f: T => U): RDD[U] 其中 U 是函数的返回类型。

    我将在很大程度上假设如果您要使用 Java 模拟框架来完成这项工作,请假设 map 的签名是 public <U> RDD<U> map(scala.Function1<T, U>, scala.reflect.ClassTag<U>);

    希望这会奏效。

    【讨论】:

    • 非常感谢! Jmock 将为 ClassTag 获取第二个参数列表。这是我所做的,这次奏效了:oneOf(customerRdd).map( with(Expectations.any(classOf[Customer => TransformedCustomer])) )( with(Expectations.anything()) )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 2014-10-06
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多