All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.mockito.strictMock.kt Maven / Gradle / Ivy

The newest version!
package name.remal.mockito

import com.nhaarman.mockito_kotlin.KStubbing
import com.nhaarman.mockito_kotlin.mock
import org.mockito.Incubating
import org.mockito.Mockito.RETURNS_SMART_NULLS
import org.mockito.invocation.InvocationOnMock
import org.mockito.listeners.InvocationListener
import org.mockito.mock.SerializableMode
import org.mockito.stubbing.Answer
import kotlin.reflect.KClass

inline fun  strictMock(
    extraInterfaces: Array>? = null,
    name: String? = null,
    spiedInstance: Any? = null,
    serializable: Boolean = false,
    serializableMode: SerializableMode? = null,
    verboseLogging: Boolean = false,
    invocationListeners: Array? = null,
    stubOnly: Boolean = false,
    @Incubating useConstructor: Boolean = false,
    @Incubating outerInstance: Any? = null,
    stubbing: KStubbing.(T) -> Unit
): T {
    val answer = StrictMockAnswer()
    val mock = mock(
        extraInterfaces = extraInterfaces,
        name = name,
        spiedInstance = spiedInstance,
        defaultAnswer = answer,
        serializable = serializable,
        serializableMode = serializableMode,
        verboseLogging = verboseLogging,
        invocationListeners = invocationListeners,
        stubOnly = stubOnly,
        useConstructor = useConstructor,
        outerInstance = outerInstance,
        stubbing = stubbing
    )
    answer.enabled = true
    return mock
}

class StrictMockAnswer(private val delegate: Answer? = null) : Answer {

    var enabled: Boolean = false

    override fun answer(invocation: InvocationOnMock?): Any {
        if (enabled) throw AssertionError("Unexpected invocation: $invocation")
        return (delegate ?: RETURNS_SMART_NULLS).answer(invocation)
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy