
name.remal.mockito.strictMock.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common Show documentation
Show all versions of common Show documentation
Java & Kotlin tools: test: common
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