0

I have a custom class:

class MyClass {
    var name = ""

    fun changeName(newName: String) {
        name = newName
    }
}

and my testing class:

@Test
fun testVerifyMock() {
    val instance: MyClass = mock()

    instance.changeName("newname")

    Assert.assertEquals("newname", instance.name)
}

I'm faily new to Unit Tests and I'm kinda stuck, can someone please point me to why I get this error:

java.lang.AssertionError: 
Expected :newname
Actual   :null

Basically the call instance.changeName("newname") doesn't seem to be changing the name since it's always null

TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50

1 Answers1

2

Mockito mocks just ignore what you pass to their methods unless you explicitly tell them what to do. In the case of changeName, the parameter is just ignored and therefore the name will remain null. I don't see why you would use a mock here anyway, so just change to:

val instance = MyClass()
...

Here's a post on "when to use mock".

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196