1

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 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.