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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BDDMockito functions #142

Merged
merged 1 commit into from
Dec 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}