【发布时间】:2021-05-08 05:41:12
【问题描述】:
我的代码使用monotonically_increasing_id函数是scala
val df = List(("oleg"), ("maxim")).toDF("first_name")
.withColumn("row_id", monotonically_increasing_id)
我想在我的单元测试中模拟它,以便它返回整数 0, 1, 2, 3, ...
在我的 spark-shell 中,它返回所需的结果。
scala> df.show
+----------+------+
|first_name|row_id|
+----------+------+
| oleg| 0|
| maxim| 1|
+----------+------+
但在我的 scala 应用程序中,结果是不同的。
如何模拟列函数?
【问题讨论】:
-
我不确定我是否理解您的问题。您能否提供更多详细信息,例如您尝试测试的代码 sn-p?
-
@Oli 我编辑了我的答案
-
他们说Don't mock type you don't own - 这听起来像是一个很好的例子,为什么你不应该这样做 - 不能保证 monotonically_increasing_id 会返回连续的数字。
-
@user10958683 我看到了这个答案。它与嘲笑的感觉相矛盾:stackoverflow.com/questions/2665812/what-is-mocking。此外,我不在乎我的生产代码中的数字是否是连续的,只要它们是唯一的。我需要在单元测试中预测结果
标签: scala unit-testing apache-spark