0

I have a code like

  test("mockito test") {
    class ToTest {
      def run(maybe: Option[Int], q: Option[Int] = None): Int = 42
    }

    val mockTest = mock[ToTest]
    when(mockTest.run(None, None)).thenReturn(98)
    mockTest.run(None)
    verify(mockTest, times(1)).run(None, None)
  }

Which fails with

[info] - mockito test *** FAILED ***
[info]   org.mockito.exceptions.verification.junit.ArgumentsAreDifferent: Argument(s) are different! Wanted:
[info] toTest$1.run(None, None);
[info] -> at xxx$$anonfun$3.apply$mcV$sp(xxx.scala:55)
[info] Actual invocation has different arguments:
[info] toTest$1.run(None, null);

Or another scenario

  test("mockito test") {
    class ToTest {
      def run(maybe: Option[Int], q: Int = 5): Int = 42
    }

    val mockTest = mock[ToTest]
    when(mockTest.run(None, 5)).thenReturn(101)
    mockTest.run(None)
    verify(mockTest, times(1)).run(None, 5)
  }

which fails with

[info] - mockito test *** FAILED ***
[info]   org.mockito.exceptions.verification.junit.ArgumentsAreDifferent: Argument(s) are different! Wanted:
[info] toTest$1.run(None, 5);
[info] -> at xxx$$anonfun$3.apply$mcV$sp(xxx.scala:55)
[info] Actual invocation has different arguments:
[info] toTest$1.run(None, 0);

I guess it's because there are no default parameters in Java. Is there any workaround for it?

Thank you.

kikulikov
  • 2,512
  • 4
  • 29
  • 45

1 Answers1

0

I guess that's because of CGLIB (or Byte Buddy in case of 2.0 beta) generates the code in that case, not Scala compiler, hence the default parameters will be always null.

A workaround could be (at least in some cases) to use spy instead:

val mockTest = spy(classOf[ToTest])

Sorry, no sugar syntax for it in ScalaTest.

MirMasej
  • 1,592
  • 12
  • 17