Skip to content

Commit

Permalink
Merge pull request #90 from nhaarman/ongoingstubbing-dothrow
Browse files Browse the repository at this point in the history
Added doThrow methods to OngoingStubbing
  • Loading branch information
nhaarman authored Oct 13, 2016
2 parents 85bb5e4 + 94b3c65 commit fd98501
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ infix fun <T> OngoingStubbing<T>.doReturn(t: T): OngoingStubbing<T> = thenReturn
fun <T> OngoingStubbing<T>.doReturn(t: T, vararg ts: T): OngoingStubbing<T> = thenReturn(t, *ts)
inline infix fun <reified T> OngoingStubbing<T>.doReturn(ts: List<T>): OngoingStubbing<T> = thenReturn(ts[0], *ts.drop(1).toTypedArray())

infix fun <T> OngoingStubbing<T>.doThrow(t: Throwable): OngoingStubbing<T> = thenThrow(t)
fun <T> OngoingStubbing<T>.doThrow(t: Throwable, vararg ts: Throwable): OngoingStubbing<T> = thenThrow(t, *ts)
infix fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java)
fun <T> OngoingStubbing<T>.doThrow(t: KClass<out Throwable>, vararg ts: KClass<out Throwable>): OngoingStubbing<T> = thenThrow(t.java, *ts.map { it.java }.toTypedArray())

fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!!
fun never(): VerificationMode = Mockito.never()!!
fun <T : Any> notNull(): T? = Mockito.notNull()
Expand Down
75 changes: 75 additions & 0 deletions mockito-kotlin/src/test/kotlin/MockitoTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.nhaarman.expect.expect
import com.nhaarman.expect.expectErrorWithMessage
import com.nhaarman.expect.fail
import com.nhaarman.mockito_kotlin.*
import org.junit.Test
import org.mockito.exceptions.base.MockitoAssertionError
Expand Down Expand Up @@ -376,6 +377,80 @@ class MockitoTest {
expect(result).toBeTheSameAs(mock)
}

@Test
fun testMockStubbing_doThrow() {
/* Given */
val mock = mock<Methods> { mock ->
on { builderMethod() } doThrow IllegalArgumentException()
}

try {
/* When */
mock.builderMethod()
fail("No exception thrown")
} catch(e: IllegalArgumentException) {
}
}

@Test
fun testMockStubbing_doThrowClass() {
/* Given */
val mock = mock<Methods> { mock ->
on { builderMethod() } doThrow IllegalArgumentException::class
}

try {
/* When */
mock.builderMethod()
fail("No exception thrown")
} catch(e: IllegalArgumentException) {
}
}

@Test
fun testMockStubbing_doThrowVarargs() {
/* Given */
val mock = mock<Methods> { mock ->
on { builderMethod() }.doThrow(IllegalArgumentException(), UnsupportedOperationException())
}

try {
/* When */
mock.builderMethod()
fail("No exception thrown")
} catch(e: IllegalArgumentException) {
}

try {
/* When */
mock.builderMethod()
fail("No exception thrown")
} catch(e: UnsupportedOperationException) {
}
}

@Test
fun testMockStubbing_doThrowClassVarargs() {
/* Given */
val mock = mock<Methods> { mock ->
on { builderMethod() }.doThrow(IllegalArgumentException::class, UnsupportedOperationException::class)
}

try {
/* When */
mock.builderMethod()
fail("No exception thrown")
} catch(e: IllegalArgumentException) {
}

try {
/* When */
mock.builderMethod()
fail("No exception thrown")
} catch(e: UnsupportedOperationException) {
}
}

@Test
fun doReturn_withSingleItemList() {
/* Given */
Expand Down

0 comments on commit fd98501

Please sign in to comment.