Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to mock method with optional default arguments #545

Open
ewerkema opened this issue Dec 10, 2024 · 0 comments
Open

Unable to mock method with optional default arguments #545

ewerkema opened this issue Dec 10, 2024 · 0 comments

Comments

@ewerkema
Copy link

ewerkema commented Dec 10, 2024

We are not able to mock methods that use optional default arguments. This problem occurs on Java 21 and using mockito-scala 1.17.37:

test("bla") {
  trait Mockable {
    def doTheThing(a: Option[Int] = None): UIO[Int]
  }

  val mockable = mock[Mockable]
  when(mockable.doTheThing()).thenReturn(ZIO.succeed(43))

  for {
    _ <- mockable.doTheThing()
  } yield assertCompletes
}

The error it produces is the following:

  - TestFacility - bla
    Exception in thread "zio-fiber-252" org.mockito.exceptions.verification.SmartNullPointerException: 
    You have a NullPointerException here:
    -> at org.scalactic.DefaultPrettifier.prettifyCollection(Prettifier.scala:188)
    because this method call was *not* stubbed correctly:
    -> at scala.Option.orElse(Option.scala:477)
    mockable$1.doTheThing$default$1();
    
    	at org.scalactic.DefaultPrettifier.prettifyCollection(Prettifier.scala:188)
    	at org.scalactic.DefaultPrettifier.prettify(Prettifier.scala:218)
    	at org.scalactic.DefaultPrettifier.apply(Prettifier.scala:223)
    	at TestFacilitySpec$.$anonfun$spec$37(TestFacilitySpec.scala:125)
    	at TestFacilitySpec.spec(TestFacilitySpec.scala:116)

It does seem to work when updating mockito-core to 5.14.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant