Skip to content

Mocking an interface with non-nullable properties sets the properties as null #372

@jordigarcl

Description

@jordigarcl

It looks like Mockito is using null as a default value for properties in a interface even though they are non-nullable.

Reproduce:

interface Dog {
  val name: String
}

class DogCaller {
  fun callDog(dog: Dog) = callSomeone(dog.name)
  private fun callSomeone(name: String) = "Hey $name come here!"
}


class DogCallerTests {

  private val dogCaller = DogCaller()

  @Test
  fun `dog caller should call dog by its name`() {
    val beagle: Dog = mock()

    // This makes the test pass 
    // whenever(beagle.name).thenReturn("Firulais")
    
    dogCaller.callDog(beagle)

    verify(beagle).name // This fails cause it detects dog.name : String = null !
  }
}

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