How to mock inline function with lambda using Mockito

1.5k Views Asked by At

Have a problem with mocking the inline function with lambda. Have class UserController, trying to mock tracer with function createSpan.

UserController class:

class UserController(private val tracer:Tracer) {
  fun subscribeUser() {
    tracer.createSpan("GraphDB: subscribedUser") {
       do something...
    }
  }
}

createSpan inline function:

inline fun <T> Tracer.createSpan(operationName: String, block: SpanContext.() -> T): T =
    buildSpan(operationName)
        .start()
        .useOn(this, block)

Tried:

 Mockito.`when`(mockTracer.createSpan<Any>(any(), any())).thenReturn(mockSpanContext)  

returns org.mockito.exceptions.misusing.InvalidUseOfMatchersException

 Mockito.`when`(mockTracer.createSpan<SpanContext>("GraphDB: subscribedUser", any())).thenReturn(mockSpanContext)

returns java.lang.NullPointerException

Tried different approaches but return only org.mockito.exceptions.misusing.InvalidUseOfMatchersException or java.lang.NullPointerException.

How can I mock this inline function with a lambda or just ignore it, because I don't want to test this function? Any help appreciated.

0

There are 0 best solutions below