Skip to content

Commit fd98501

Browse files
authored
Merge pull request #90 from nhaarman/ongoingstubbing-dothrow
Added doThrow methods to OngoingStubbing
2 parents 85bb5e4 + 94b3c65 commit fd98501

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ infix fun <T> OngoingStubbing<T>.doReturn(t: T): OngoingStubbing<T> = thenReturn
9999
fun <T> OngoingStubbing<T>.doReturn(t: T, vararg ts: T): OngoingStubbing<T> = thenReturn(t, *ts)
100100
inline infix fun <reified T> OngoingStubbing<T>.doReturn(ts: List<T>): OngoingStubbing<T> = thenReturn(ts[0], *ts.drop(1).toTypedArray())
101101

102+
infix fun <T> OngoingStubbing<T>.doThrow(t: Throwable): OngoingStubbing<T> = thenThrow(t)
103+
fun <T> OngoingStubbing<T>.doThrow(t: Throwable, vararg ts: Throwable): OngoingStubbing<T> = thenThrow(t, *ts)
104+
infix fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java)
105+
fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>, vararg ts: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java, *ts.map { it.java }.toTypedArray())
106+
102107
fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!!
103108
fun never(): VerificationMode = Mockito.never()!!
104109
fun <T : Any> notNull(): T? = Mockito.notNull()

mockito-kotlin/src/test/kotlin/MockitoTest.kt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import com.nhaarman.expect.expect
22
import com.nhaarman.expect.expectErrorWithMessage
3+
import com.nhaarman.expect.fail
34
import com.nhaarman.mockito_kotlin.*
45
import org.junit.Test
56
import org.mockito.exceptions.base.MockitoAssertionError
@@ -376,6 +377,80 @@ class MockitoTest {
376377
expect(result).toBeTheSameAs(mock)
377378
}
378379

380+
@Test
381+
fun testMockStubbing_doThrow() {
382+
/* Given */
383+
val mock = mock<Methods> { mock ->
384+
on { builderMethod() } doThrow IllegalArgumentException()
385+
}
386+
387+
try {
388+
/* When */
389+
mock.builderMethod()
390+
fail("No exception thrown")
391+
} catch(e: IllegalArgumentException) {
392+
}
393+
}
394+
395+
@Test
396+
fun testMockStubbing_doThrowClass() {
397+
/* Given */
398+
val mock = mock<Methods> { mock ->
399+
on { builderMethod() } doThrow IllegalArgumentException::class
400+
}
401+
402+
try {
403+
/* When */
404+
mock.builderMethod()
405+
fail("No exception thrown")
406+
} catch(e: IllegalArgumentException) {
407+
}
408+
}
409+
410+
@Test
411+
fun testMockStubbing_doThrowVarargs() {
412+
/* Given */
413+
val mock = mock<Methods> { mock ->
414+
on { builderMethod() }.doThrow(IllegalArgumentException(), UnsupportedOperationException())
415+
}
416+
417+
try {
418+
/* When */
419+
mock.builderMethod()
420+
fail("No exception thrown")
421+
} catch(e: IllegalArgumentException) {
422+
}
423+
424+
try {
425+
/* When */
426+
mock.builderMethod()
427+
fail("No exception thrown")
428+
} catch(e: UnsupportedOperationException) {
429+
}
430+
}
431+
432+
@Test
433+
fun testMockStubbing_doThrowClassVarargs() {
434+
/* Given */
435+
val mock = mock<Methods> { mock ->
436+
on { builderMethod() }.doThrow(IllegalArgumentException::class, UnsupportedOperationException::class)
437+
}
438+
439+
try {
440+
/* When */
441+
mock.builderMethod()
442+
fail("No exception thrown")
443+
} catch(e: IllegalArgumentException) {
444+
}
445+
446+
try {
447+
/* When */
448+
mock.builderMethod()
449+
fail("No exception thrown")
450+
} catch(e: UnsupportedOperationException) {
451+
}
452+
}
453+
379454
@Test
380455
fun doReturn_withSingleItemList() {
381456
/* Given */

0 commit comments

Comments
 (0)