Skip to content

Unable to mock method with optional default arguments #545

@ewerkema

Description

@ewerkema

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

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