Search code examples
scalascalatestscalamock

ScalaMock Stubbing with default parameters


I'm trying to mock a function like

def foo(x: A, y: B, z: C = blah)

where blah is a java connection object that I don't want to create on the spot

However when I try to stub it like

    (object.foo _)
  .stubs(a, b)

It errors out and says overloaded method value stubs with alternatives... because it's looking for the third parameter. Is there anyway to get around this.


Solution

  • I agree with Matt, but want to point out there is a wildcard syntax in ScalaMock (*) - http://scalamock.org/user-guide/matching/

    trait Foo {
      def foo(x: Int, y: Int, z: Int = 0): Int
    }
    
    val a: Int = ???
    val b: Int = ???
    val m = mock[Foo]
    
    m.foo _ stubs(a, b, *)