在使用Mockito定义mock的行为时,测试等于RDD(Test equals for RDD while defining mock's behavior with Mockito)

编程入门 行业动态 更新时间:2024-10-22 15:24:07
在使用Mockito定义mock的行为时,测试等于RDD(Test equals for RDD while defining mock's behavior with Mockito)

我正在使用Mockito编写一些java spark程序的单元测试,当我尝试定义模拟对象的方法的行为时,我遇到了问题:

when(mock.method(someRDD)).thenReturn(0);

由于RDD没有重新实现equals()函数,因此模拟行为只能在方法中传递的rdd与此“someRDD”的引用相同。

我想知道是否有任何方法可以在模拟方法中自定义Mockito中的“equals()”检查行为? 或者我应该使用其他一些模拟框架代替?

I am writing unit test for some java spark program with Mockito, I got problem when I try to define the behavior of the method of a mocked object like:

when(mock.method(someRDD)).thenReturn(0);

Since RDD doesn't re-implements equals() function, the mocked behavior works only the rdd passed in the methods is the same reference of this "someRDD".

I wonder if there is any way to customize the "equals()" check behavior in Mockito in mocking methods ? or maybe I should use some other mock framework instead?

最满意答案

您可以编写自己的ArgumentMatcher来关联传递的参数和您期望的参数。 假设它只是RDD字段之间的直接比较,您可以使用Mockito内置的refEq匹配器,它使用反射并且只是单独比较每个字段:

when(mock.method(refEq(someRDD))).thenReturn(0);

You can write your own ArgumentMatcher to correlate between the passed argument and what you expect. Assuming it's just a straight-forward comparison between the RDD's fields, you can use Mockito's built in refEq matcher that uses reflection and just compares each field individually:

when(mock.method(refEq(someRDD))).thenReturn(0);

更多推荐

本文发布于:2023-08-05 04:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427225.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   测试   mock   Mockito   RDD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!