Mockito for Objects in Scala -
i'm using scala 2.10, specs2 , mockito. want mock scala.io.source.fromurl(). issue seems fromurl() function in io.source's object.
val m = mock[io.source] m.fromurl returns io.source.fromstring("some random string.")
it's pretty straightforward mock in unit test. why isn't working?
thanks!
instead of mocking it, try spying
follows:
val m = spy(io.source)
or mock follows:
val m = mock[io.source.type]
but how using source
in class testing? if had example class so:
class myclass{ def foo = { io.source.dosomething //i know dosomething not on source, call not important } }
then in order take advantage of mocking/spying, you'd have structure class so:
class myclass{ val source = io.source def foo = { source.dosomething } }
and test have this:
val mocksource = mock[io.source.type] val totest = new myclass{ override val source = mocksource }
in java world, static methods bane of mocking. in scala world, calls objects can troublesome deal unit tests. if follow code above, should able mock out object based dependency in class.
Comments
Post a Comment