Skip to content

Commit

Permalink
Add BDDMockito functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaarman committed Dec 19, 2016
1 parent 17056aa commit 210c733
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.nhaarman.mockito_kotlin

import org.mockito.BDDMockito

fun <T> given(methodCall: T): BDDMockito.BDDMyOngoingStubbing<T> = BDDMockito.given(methodCall)
fun <T> given(methodCall: () -> T) = given(methodCall())

fun <T> then(mock: T): BDDMockito.Then<T> = BDDMockito.then(mock)

infix fun <T> BDDMockito.BDDMyOngoingStubbing<T>.willAnswer(value: () -> T): BDDMockito.BDDMyOngoingStubbing<T> = willAnswer { value() }
infix fun <T> BDDMockito.BDDMyOngoingStubbing<T>.willReturn(value: () -> T): BDDMockito.BDDMyOngoingStubbing<T> = willReturn(value())
infix fun <T> BDDMockito.BDDMyOngoingStubbing<T>.willThrow(value: () -> Throwable): BDDMockito.BDDMyOngoingStubbing<T> = willThrow(value())

103 changes: 103 additions & 0 deletions mockito-kotlin/src/test/kotlin/test/BDDMockitoTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package test

import com.nhaarman.expect.expect
import com.nhaarman.mockito_kotlin.*
import org.junit.Test

class BDDMockitoTest {

@Test
fun given_willReturn_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given(mock.stringResult()).willReturn("Test")

/* Then */
expect(mock.stringResult()).toBe("Test")
}

@Test
fun givenLambda_willReturn_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given { mock.stringResult() }.willReturn("Test")

/* Then */
expect(mock.stringResult()).toBe("Test")
}

@Test
fun given_willReturnLambda_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given(mock.stringResult()).willReturn { "Test" }

/* Then */
expect(mock.stringResult()).toBe("Test")
}

@Test
fun givenLambda_willReturnLambda_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given { mock.stringResult() } willReturn { "Test" }

/* Then */
expect(mock.stringResult()).toBe("Test")
}

@Test
fun given_willAnswer_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given(mock.stringResult()).willAnswer { "Test" }

/* Then */
expect(mock.stringResult()).toBe("Test")
}

@Test
fun given_willAnswerInfix_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given(mock.stringResult()) willAnswer { "Test" }

/* Then */
expect(mock.stringResult()).toBe("Test")
}

@Test(expected = IllegalStateException::class)
fun given_willThrowInfix_properlyStubs() {
/* Given */
val mock = mock<Methods>()

/* When */
given(mock.stringResult()) willThrow { IllegalStateException() }
mock.stringResult()
}

@Test
fun then() {
/* Given */
val mock = mock<Methods>()
whenever(mock.stringResult()).thenReturn("Test")

/* When */
mock.stringResult()

/* Then */
then(mock).should().stringResult()
}
}

0 comments on commit 210c733

Please sign in to comment.