scala - How can I use MockitoSugar to mock an object (singleton) method? -
is there way in scala use mockitosugar
in order mock method of object
scala singleton?
your best bet deal singletons mocking first little rework on structure of singleton itself. use trait define operations , have object extend trait so:
trait fooable{ def dofoo:string = "foo" } object foo extends fooable
then, in class needs foo
object, declare input or can set (di), decalare trait instead this:
class myfoouser(foo:fooable = foo){ }
that way default uses object, when constructing tests, can give mocked fooable
instead. there bunch of ways handle getting fooable
classes (this one) , that's not in scope answer. answer using trait first define methods , having object extend trait , referring trait in class needs it. allow mock effectively.
Comments
Post a Comment