Skip to content

Meaningful error when calling an unstubbed method with a non-nullable return type #332

Open
@Mahoney

Description

@Mahoney

Mockito returns null for any invocation where no expectation was set up.

If the method return type is non-nullable in Kotlin you get an NPE at a point it shouldn't really be possible given the type system's guarantees.

It would be good if you got a meaningful exception ("Unexpected invocation of method < signature > which has a non-nullable return type with arguments < args > on mock < mock >") at the line the method was called.

package mockitotests

interface X {
  fun y(i: Int): String
}

class MockitoTest : StringSpec({

  "null safety" {

    val aMock: X = mock()

    val error = shouldThrow<Throwable> {
      aMock.y(3)
    }

    error.message shouldBe "Unexpected invocation of method fun mockitotests.X.y(kotlin.Int): kotlin.String which has non-nullable return type with arguments [3] on mock $aMock"
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions