Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding throwsMany exception #955

Merged
merged 4 commits into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions modules/mockk-dsl/src/commonMain/kotlin/io/mockk/API.kt
Expand Up @@ -2133,6 +2133,9 @@ class MockKStubScope<T, B>(

infix fun throws(ex: Throwable) = answers(ThrowingAnswer(ex))

infix fun throwsMany(exList: List<Throwable>): MockKAdditionalAnswerScope<T, B> =
this answers (ManyAnswersAnswer(exList.map { ThrowingAnswer(it) }))

infix fun answers(answer: MockKAnswerScope<T, B>.(Call) -> T) =
answers(FunctionAnswer { MockKAnswerScope<T, B>(lambda, it).answer(it) })

Expand Down Expand Up @@ -2183,6 +2186,9 @@ class MockKAdditionalAnswerScope<T, B>(

infix fun andThenThrows(ex: Throwable) = andThenAnswer(ThrowingAnswer(ex))

infix fun andThenThrowsMany(exList: List<Throwable>) =
andThenAnswer(ManyAnswersAnswer(exList.map { ThrowingAnswer(it) }))

@Deprecated("Use andThenAnswer instead of andThen.")
infix fun andThen(answer: MockKAnswerScope<T, B>.(Call) -> T) =
andThenAnswer(FunctionAnswer { MockKAnswerScope<T, B>(lambda, it).answer(it) })
Expand Down
Expand Up @@ -123,6 +123,53 @@ class AdditionalAnswerTest {
}
}

@Test
fun andThrowsMany() {
val exceptions = listOf(
IllegalArgumentException("error1"),
NullPointerException("error2"),
NoSuchElementException("error3")
)

every { mock.op(any()) } throwsMany exceptions

assertFailsWith(IllegalArgumentException::class) {
mock.op(2)
}
assertFailsWith(NullPointerException::class) {
mock.op(3)
}
assertFailsWith(NoSuchElementException::class) {
mock.op(3)
}
assertFailsWith(NoSuchElementException::class) {
mock.op(4)
}
}

@Test
fun andThenThrowsMany() {
val exceptions = listOf(
IllegalArgumentException("error1"),
NullPointerException("error2"),
NoSuchElementException("error3")
)
every { mock.op(any()) } returns 3 andThenThrowsMany exceptions andThen 2

assertEquals(3, mock.op(1))
assertFailsWith(IllegalArgumentException::class) {
mock.op(2)
}
assertFailsWith(NullPointerException::class) {
mock.op(3)
}
assertFailsWith(NoSuchElementException::class) {
mock.op(3)
}
assertEquals(2, mock.op(4))
assertEquals(2, mock.op(5))
}

@Test
fun arbitraryLengthCheck() {
every {
Expand Down