How can I mock dependend traits with mockito? I have two traits:
trait A {
def a = 1
}
trait B extends A {
def b = {
// do things
a
// do things
}
}
Now, I want to test Trait B. And I want to verify, that A.a() gets called:
class SmallTest extends FunSuite with MockitoSugar {
test("prove, that a gets called") {
val mockA = mock[A]
val b = new B with mockA {} // This won't compile
b.b
verify(mockA).a
}
}
This example won't compile. But how would I "inject" my mock otherwise?
Ok, found a way. This will work. Maybe a bit unhandy, if there are a lot methods, I have to override... In this example I added method parameters to the method I want to mock, to show the repetition, which doesn't look very nice.
trait A {
def a(i: Int) = i + 1
}
trait B extends A {
def b(j: Int) = {
// do things
a(j)
// do things
}
}
test("prove, that a gets called") {
val mockA = mock[A]
val b = new B {
override def a(j: Int) = mockA.a(j)
}
b.b(1)
verify(mockA).a(1)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With