diff --git a/kotlin-mockito/pom.xml b/kotlin-mockito/pom.xml index 47f4f8947f..abb43f4109 100644 --- a/kotlin-mockito/pom.xml +++ b/kotlin-mockito/pom.xml @@ -20,7 +20,7 @@ org.mockito - mockito-all + mockito-core ${mockito.version} test @@ -30,9 +30,14 @@ ${junit.version} test + + com.nhaarman + mockito-kotlin + ${mockito-kotlin.version} + test + - ${project.basedir}/src/main/kotlin ${project.basedir}/src/test/kotlin @@ -106,9 +111,10 @@ - 1.9.5 + 2.8.9 4.12 1.1.2-4 + 1.5.0 3.5.1 diff --git a/kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTest.kt b/kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTest.kt index 3998d6a92e..ab08273686 100644 --- a/kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTest.kt +++ b/kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTest.kt @@ -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) } } \ No newline at end of file diff --git a/kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTestMockitoKotlin.kt b/kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTestMockitoKotlin.kt new file mode 100644 index 0000000000..1ff4e20c61 --- /dev/null +++ b/kotlin-mockito/src/test/kotlin/com/baeldung/kotlin/LendBookManagerTestMockitoKotlin.kt @@ -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() + + 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) + } +} \ No newline at end of file