Skip to content

How to handle default arguments #17

@jetjager

Description

@jetjager

Hi, I struggle with finding a method for mocking, when the method has default arguments.
The following fails with java.lang.AssertionError: Unexpected method call Foo.bar$default$2()

//> using dep "org.scalatest::scalatest:3.2.17"
//> using dep "org.scalatestplus::easymock-5-1:3.2.17.0"
//> using dep "org.easymock:easymock:5.1.0"
import org.scalatestplus.easymock.EasyMockSugar
import org.scalatest.funsuite.AnyFunSuite
import org.easymock.EasyMock.{capture, newCapture}

trait Foo {
  def bar(a: String, b: Int = 0): String
}

class EasyMockTest extends AnyFunSuite with EasyMockSugar {

  test("test") {
    val foo = mock[Foo]

    val aCapture = newCapture[String]
    val bCapture = newCapture[Int]
    expecting {
      foo.bar(capture(aCapture), capture(bCapture)).andStubReturn("bar")
    }

    whenExecuting(foo) {
      val bar1 = foo.bar("a", 0)
      assert(bar1 == "bar")

      val bar2 = foo.bar("a")
      assert(bar2 == "bar")

    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions