1
votes

I have this Java test with Mockito:

public class PersistentNodeDeserializerTests {

    @Test
    public void userInfoPersistentNodeDeserializer() {
        PersistentNode node = mock(PersistentNode.class);
        when(node.stringChild("username")).thenReturn("cliff12");
        //more stuff
    }

}

PersistentNode is a Kotlin class:

open class PersistentNode(private val path: PersistentNodePath, val content: Any) {

    val stringPath: String
        get() = path.get()

    val key: String
        get() {
            val parts = stringPath.split("/");
            return parts[parts.size - 1];
        }

    val mapContent: Map<String, Any>
        get() {
            return content as HashMap<String, Any>
        }

    fun stringChild(child: String): String {
        return mapContent.get(child) as String
    }

}

I get this error:

kotlin.TypeCastException: null cannot be cast to non-null type java.util.HashMap

How can I mock the property stringChild properly?

1

1 Answers

4
votes

this library may solve your issue https://github.com/nhaarman/mockito-kotlin

EDIT: sorry, didn't realize you were using a Java test. If it's an option, try writing your test in kotlin too