TootsieRockNRoll
TootsieRockNRoll

Reputation: 3288

Android - mocking issue

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

Upvotes: 0

Views: 42

Answers (1)

s1m0nw1
s1m0nw1

Reputation: 82077

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".

Upvotes: 2

Related Questions