* BAEL-809: Add Kotlin Mockito supporting code

* BAEL-809: Add references to kotlin-mockito library
This commit is contained in:
Felipe Reis 2017-06-25 21:22:52 +10:00 committed by maibin
parent 7c4016e056
commit 526fd057ce
3 changed files with 42 additions and 7 deletions

View File

@ -20,7 +20,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
@ -30,9 +30,14 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nhaarman</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>${mockito-kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
@ -106,9 +111,10 @@
</build>
<properties>
<mockito.version>1.9.5</mockito.version>
<mockito.version>2.8.9</mockito.version>
<junit.version>4.12</junit.version>
<kotlin.version>1.1.2-4</kotlin.version>
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
<maven-compiler.version>3.5.1</maven-compiler.version>
</properties>

View File

@ -1,9 +1,6 @@
package com.baeldung.kotlin;
import org.junit.Assert
import org.junit.Test
import java.lang.IllegalStateException
import org.mockito.InjectMocks
import org.mockito.Mockito
class LibraryManagementTest {
@ -28,6 +25,6 @@ class LibraryManagementTest {
manager.checkout(100, 1)
Mockito.`verify`(mockBookService).lend(100, 1)
Mockito.verify(mockBookService).lend(100, 1)
}
}

View File

@ -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)
}
}