0

I am trying mockito to mock a getValue function that consumes a java class as parameter.

To simplify, i did the following test:

@Test
fun test1() {
    val map = HashMap<String,Any>()
    val v:Long = 1L 
    map["K"]= v

    println(map["K"]!!::class.java) //prints class java.lang.Long
    println(Long::class.java) //prints long

    val dss = Mockito.mock(DataSnapshot::class.java)

    Mockito.`when`(dss.getValue( map["K"]!!::java.class))
            .thenReturn( map["K"]!!)

    //production code uses calls the function like this but it fails to get the value. Returns null;
    Assert.assertEquals( map["K"],dss.getValue(Long::class.java))
}

As the prints show, the type in map["K"]!!::class.java is different from Long::class.java.

If i mock the method using the type inline it works:

Mockito.`when`(dss.getValue( Long::class.java))
            .thenReturn( map["K"]!!)

How can i mock the method in a way that the type parameter hasn't to be determined by a long switch logic?

Some insides in kotlin and java types could help.

MiguelSlv
  • 14,067
  • 15
  • 102
  • 169

1 Answers1

2

If you're asserting against java.lang.Long, use Long?::class.java. Long::class.java will give you the primitive long on the JVM. Nullable Long instead maps to the boxed version - java.long.Long.

Egor
  • 39,695
  • 10
  • 113
  • 130