我正在使用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);更多推荐
发布评论