mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
Make rigorousMock callRealMethod by default for concrete methods. (#2159)
This commit is contained in:
@ -24,6 +24,7 @@ import net.corda.nodeapi.internal.crypto.X509Utilities
|
||||
import net.corda.nodeapi.internal.serialization.amqp.AMQP_ENABLED
|
||||
import org.mockito.Mockito.mock
|
||||
import org.mockito.internal.stubbing.answers.ThrowsException
|
||||
import java.lang.reflect.Modifier
|
||||
import java.nio.file.Files
|
||||
import java.security.KeyPair
|
||||
import java.security.PublicKey
|
||||
@ -180,11 +181,16 @@ class UndefinedMockBehaviorException(message: String) : RuntimeException(message
|
||||
|
||||
inline fun <reified T : Any> rigorousMock() = rigorousMock(T::class.java)
|
||||
/**
|
||||
* Create a Mockito mock that has [UndefinedMockBehaviorException] as the default behaviour of all methods.
|
||||
* @param T the type to mock. Note if you want to use [com.nhaarman.mockito_kotlin.doCallRealMethod] on a Kotlin interface,
|
||||
* Create a Mockito mock that has [UndefinedMockBehaviorException] as the default behaviour of all abstract methods,
|
||||
* and [org.mockito.invocation.InvocationOnMock.callRealMethod] as the default for all concrete methods.
|
||||
* @param T the type to mock. Note if you want concrete methods of a Kotlin interface to be invoked,
|
||||
* it won't work unless you mock a (trivial) abstract implementation of that interface instead.
|
||||
*/
|
||||
fun <T> rigorousMock(clazz: Class<T>): T = mock(clazz) {
|
||||
// Use ThrowsException to hack the stack trace, and lazily so we can customise the message:
|
||||
ThrowsException(UndefinedMockBehaviorException("Please specify what should happen when '${it.method}' is called, or don't call it. Args: ${Arrays.toString(it.arguments)}")).answer(it)
|
||||
if (Modifier.isAbstract(it.method.modifiers)) {
|
||||
// Use ThrowsException to hack the stack trace, and lazily so we can customise the message:
|
||||
ThrowsException(UndefinedMockBehaviorException("Please specify what should happen when '${it.method}' is called, or don't call it. Args: ${Arrays.toString(it.arguments)}")).answer(it)
|
||||
} else {
|
||||
it.callRealMethod()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user