From 94b3c652833caf4066aa7203d6cabf4631eae1af Mon Sep 17 00:00:00 2001 From: Niek Haarman Date: Thu, 13 Oct 2016 13:39:52 +0200 Subject: [PATCH] Added doThrow methods to OngoingStubbing --- .../com/nhaarman/mockito_kotlin/Mockito.kt | 5 ++ mockito-kotlin/src/test/kotlin/MockitoTest.kt | 75 +++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt index a35ce024..e674d75a 100644 --- a/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt +++ b/mockito-kotlin/src/main/kotlin/com/nhaarman/mockito_kotlin/Mockito.kt @@ -99,6 +99,11 @@ infix fun OngoingStubbing.doReturn(t: T): OngoingStubbing = thenReturn fun OngoingStubbing.doReturn(t: T, vararg ts: T): OngoingStubbing = thenReturn(t, *ts) inline infix fun OngoingStubbing.doReturn(ts: List): OngoingStubbing = thenReturn(ts[0], *ts.drop(1).toTypedArray()) +infix fun OngoingStubbing.doThrow(t: Throwable): OngoingStubbing = thenThrow(t) +fun OngoingStubbing.doThrow(t: Throwable, vararg ts: Throwable): OngoingStubbing = thenThrow(t, *ts) +infix fun OngoingStubbing.doThrow(t: KClass): OngoingStubbing = thenThrow(t.java) +fun OngoingStubbing.doThrow(t: KClass, vararg ts: KClass): OngoingStubbing = thenThrow(t.java, *ts.map { it.java }.toTypedArray()) + fun mockingDetails(toInspect: Any): MockingDetails = Mockito.mockingDetails(toInspect)!! fun never(): VerificationMode = Mockito.never()!! fun notNull(): T? = Mockito.notNull() diff --git a/mockito-kotlin/src/test/kotlin/MockitoTest.kt b/mockito-kotlin/src/test/kotlin/MockitoTest.kt index 75d7cbda..65d9ed12 100644 --- a/mockito-kotlin/src/test/kotlin/MockitoTest.kt +++ b/mockito-kotlin/src/test/kotlin/MockitoTest.kt @@ -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 @@ -376,6 +377,80 @@ class MockitoTest { expect(result).toBeTheSameAs(mock) } + @Test + fun testMockStubbing_doThrow() { + /* Given */ + val mock = mock { mock -> + on { builderMethod() } doThrow IllegalArgumentException() + } + + try { + /* When */ + mock.builderMethod() + fail("No exception thrown") + } catch(e: IllegalArgumentException) { + } + } + + @Test + fun testMockStubbing_doThrowClass() { + /* Given */ + val mock = mock { 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 { 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 { 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 */