使用Mockito将Scala中的课程进行匹配 [英] Match classes in Scala with Mockito
问题描述
我正在尝试模拟Spark上下文以在调用 newAPIHadoopFile
时返回模拟的RDD.
I'm trying to mock up a Spark context to return a mocked RDD when newAPIHadoopFile
is called.
我将其设置如下:
val mockedOuterRdd = mock[RDD[(NullWritable, MyProtobuf)]]
mockedSc.newAPIHadoopFile(anyString, anyObject(),classOf[org.apache.hadoop.io.NullWritable],
classOf[MyProtobuf],anyObject()) returns mockedOuterRdd
这在编译器中很好,但是当我运行它时,我会得到
This is fine in the compiler, but when I run it I get
Invalid use of argument matchers!
5 matchers expected, 3 recorded:
-> at ...
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
有没有办法我可以在 classOf [...]
中使用类似 eq(...)
的东西(我尝试过但不起作用)>?
Is there a way I can use something like eq(...)
(which I've tried and doesn't work) with classOf[...]
?
我已经尝试将 anyObject
用于这些类,但是它会从这些类中推断出RDD的类型参数,因此它们必须正确.
I've tried using anyObject
for the classes, but it infers the type parameter for the RDD from these so they need to be right.
感谢阅读.
推荐答案
eq
适用于我,但是我想您可能遗漏了一些测试框架的细节.我使用了 scalatest
,当我尝试使用 eq
时,我一直大喊大叫我返回 boolean
而不是 Class
.但是,有一个通用的 eq
应该接管.因此,解决方案是使用完整路径:
eq
works for me, however I am guessing you might have left out some details of your testing framework. I used scalatest
and when I tried to use eq
it kept yelling at me for returning a boolean
instead of a Class
. However, there is a generic eq
that should take over. So, the solution is to use the full path:
mockedSc.newAPIHadoopFile(anyString, anyObject(),
org.mockito.Matchers.eq(classOf[org.apache.hadoop.io.NullWritable]),
org.mockito.Matchers.eq(classOf[org.apache.hadoop.io.NullWritable]), anyObject())
这篇关于使用Mockito将Scala中的课程进行匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!