1

I have a question concerning unit tests that I'm trying to achieve using Mockito in Scala. I've also looked up ScalaMock but it sounds like the feature is not provided as well. I suppose that maybe I'm looking from a narrow way to the solution and there might be a different perspective or approach to what im doing so all your opinions are welcomed.

Basically, I want to mock a function that is available to the object using implicit conversion, and I don't have any control to change how that is done. Since I'm a user to the library. The concrete example is similar to the following scenario

rdd: RDD[T] = //existing RDD
sqlContext: SQLContext = //existing sqlcontext
import sqlContext.implicits._
rdd.toDF() 
/*toDF() doesn't originally exist at RDD but is implicitly added when importing sqlContext.implicits._*/

Now In the testing, I'm mocking the rdd and the sqlContext and I want to mock the toDF() function. I Can't mock the function toDF() since it doesn't exist on the RDD level. Even if I do a simple trick, importing the mocked sqlContext.implicit._ I get an error that any function that is not publicaly available to the object can't be mocked. I even tried to mock the code that is implicitly executed until toDF() but I get stuck with Final/Pivate[in accessible] classes that I also can't mock. Your suggestions are more than welcomed. Thanks in advance :)

sthzg
  • 5,514
  • 2
  • 29
  • 50
ElSioufy
  • 162
  • 2
  • 9
  • Why would you want to mock all this stuff? What's wrong with actually using the real implicit conversion and even a real RDD? – herman Jan 27 '16 at 16:26
  • away from the fact of mocking the RDD .. this idea of mocking an implicitly wrapped function is a research thing for me that I would like to find an answer for. you can consider any other example, not this concrete one :) ... – ElSioufy Jan 28 '16 at 20:08
  • OK, I just wonder when it would ever be useful to mock those. I don't think implicit functions should access the DB, or call web services or anything like that. – herman Jan 29 '16 at 01:26
  • @herman ... in my example we implicitly convert an RDD into a dataframe and store it back "structured" to hdfs. we don't want this to happen in unit testing! Do you :) ? – ElSioufy Jan 30 '16 at 10:11
  • Maybe not. What do you want to happen in your test? I mean, what is the code that you actually want to test? – herman Jan 30 '16 at 21:11

0 Answers0