BAEL-809: Moved mockito-kotlin to existing mockito module (#2191)

* BAEL-809: Add Kotlin Mockito supporting code

* BAEL-809: Add references to kotlin-mockito library

* BAEL-809: Move kotlin-mockito to existing kotlin module

Some unused java classes that were in the kotlin-mockito
module were simply deleted instead of moved to the mockito
module.
This commit is contained in:
Felipe Reis
2017-07-02 15:08:10 +10:00
committed by maibin
parent 37360b9f29
commit 6e994240f7
11 changed files with 7 additions and 205 deletions
@@ -0,0 +1,6 @@
package com.baeldung.kotlin
interface BookService {
fun inStock(bookId: Int): Boolean
fun lend(bookId: Int, memberId: Int)
}
@@ -0,0 +1,11 @@
package com.baeldung.kotlin
class LendBookManager(val bookService:BookService) {
fun checkout(bookId: Int, memberId: Int) {
if(bookService.inStock(bookId)) {
bookService.lend(bookId, memberId)
} else {
throw IllegalStateException("Book is not available")
}
}
}
@@ -0,0 +1,30 @@
package com.baeldung.kotlin;
import org.junit.Test
import org.mockito.Mockito
class LibraryManagementTest {
@Test(expected = IllegalStateException::class)
fun whenBookIsNotAvailable_thenAnExceptionIsThrown() {
val mockBookService = Mockito.mock(BookService::class.java)
Mockito.`when`(mockBookService.inStock(100)).thenReturn(false)
val manager = LendBookManager(mockBookService)
manager.checkout(100, 1)
}
@Test
fun whenBookIsAvailable_thenLendMethodIsCalled() {
val mockBookService = Mockito.mock(BookService::class.java)
Mockito.`when`(mockBookService.inStock(100)).thenReturn(true)
val manager = LendBookManager(mockBookService)
manager.checkout(100, 1)
Mockito.verify(mockBookService).lend(100, 1)
}
}
@@ -0,0 +1,32 @@
package com.baeldung.kotlin;
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Test
class LibraryManagementTestMockitoKotlin {
@Test(expected = IllegalStateException::class)
fun whenBookIsNotAvailable_thenAnExceptionIsThrown() {
val mockBookService = mock<BookService>()
whenever(mockBookService.inStock(100)).thenReturn(false)
val manager = LendBookManager(mockBookService)
manager.checkout(100, 1)
}
@Test
fun whenBookIsAvailable_thenLendMethodIsCalled() {
val mockBookService : BookService = mock()
whenever(mockBookService.inStock(100)).thenReturn(true)
val manager = LendBookManager(mockBookService)
manager.checkout(100, 1)
verify(mockBookService).lend(100, 1)
}
}