1

Let's say that I have this:

class Dependency1 {
    def methodD1 { }
}

class Dependency2 { 
   val dependency1 = new Dependency1
}

def myMethod() {
    val a = new Dependency1

    // I want to be able to stub this
    val b = a.dependency1.methodD1()
    ...
}

I want to do something like in RR (ruby mock library):

any_instance_of(Dependency1) do | obj | 
    stub(obj) { "123" }  # this would be like stub(obj) toReturn("123") with Mockito in Scala
end

I know that there is an any method in Mockito but it's a matcher. I'm looking for something like:

stub(anyInstanceOf(Dependency1).methodD1) toReturn("123")

Is there a way to mock/stub a local dependency with Mockito/EasyMock/PowerMock/JMock ?

I'm using ScalaTest with MockitoSugar.

jmdev
  • 729
  • 1
  • 6
  • 13

1 Answers1

4

I know this is Scala, not Java; but if you have a look at the Mockito wiki article at http://code.google.com/p/mockito/wiki/MockingObjectCreation, it describes how to solve this in Java. I imagine the Scala solution will be much the same.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110