Last active
February 25, 2020 10:36
-
-
Save dadouf/f9ec2b5d44f20295fcadb0e8834ad45a to your computer and use it in GitHub Desktop.
Mockito+Kotlin: set mocks to throw unchecked exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mockito.BDDMockito | |
// Use just like a normal .willThrow: | |
// ``` | |
// given(api.fetch()).willThrowUnchecked(Exception("First error"), Exception("Second error")) | |
// ``` | |
fun <T> BDDMockito.BDDMyOngoingStubbing<T>.willThrowUnchecked(vararg throwables: Throwable) { | |
var invocationNumber = 0 | |
this.willAnswer { | |
val throwableIndex = invocationNumber++.coerceAtMost(throwables.size - 1) | |
throw throwables[throwableIndex] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment