Skip to content

Commit

Permalink
Merge pull request #174 from Tenkei/feature/issue-49-implemnent-verif…
Browse files Browse the repository at this point in the history
…ication-mode-times

Implement VerificationMode times #49
  • Loading branch information
MarkusAmshove authored Jan 29, 2020
2 parents a0b0bc4 + be463c6 commit 8f98e79
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
1. Ivan Mikhnovich - [@Murtaught](https://github.com/Murtaught) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Murtaught))
1. Jc Miñarro - [@JcMinarro](https://github.com/JcMinarro) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=JcMinarro))
1. Kshitij Patil [@Kshitij09](https://github.com/Kshitij09) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Kshitij09))
1. Keivan Esbati - [@Tenkei](https://github.com/Tenkei) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Tenkei))
13 changes: 9 additions & 4 deletions jvm/src/main/kotlin/org/amshove/kluent/Mocking.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.amshove.kluent

import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
import com.nhaarman.mockitokotlin2.verifyZeroInteractions
import com.nhaarman.mockitokotlin2.*
import org.mockito.AdditionalAnswers.*
import org.mockito.Mockito.`when`
import org.mockito.internal.util.MockUtil
Expand All @@ -12,11 +9,17 @@ import org.mockito.stubbing.Answer
import org.mockito.stubbing.OngoingStubbing
import kotlin.reflect.KClass



@Suppress("UNUSED_PARAMETER") // Backward compatibility
inline fun <reified T : Any> mock(targetClass: KClass<out T>): T = mock()

inline fun <reified T : Any> mock(): T = com.nhaarman.mockitokotlin2.mock()

infix fun VerifyKeyword.times(numInvocations: Int) = OngoingVerification(numInvocations)

infix fun <T> OngoingVerification.on(mock: T) = verify(mock, times(numInvocations))

infix fun <T> VerifyKeyword.on(mock: T) = verify(mock)

infix fun <T> VerifyNotCalledKeyword.on(mock: T) = verify(mock, never())
Expand Down Expand Up @@ -87,3 +90,5 @@ class CalledKeyword internal constructor()
class WhenKeyword internal constructor()
class VerifyNoInteractionsKeyword internal constructor()
class VerifyNoFurtherInteractionsKeyword internal constructor()

class OngoingVerification(val numInvocations: Int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.amshove.kluent.tests.mocking

import org.amshove.kluent.*
import org.amshove.kluent.tests.helpclasses.Database
import kotlin.test.Test
import kotlin.test.assertFails

class VerifyUsingTimesShould {

@Test
fun passWhenAMethodWasCalledSpecifiedTimes() {
val mock = mock(Database::class)
mock.getPerson(1)
mock.getPerson(5)
Verify on mock that mock.getPerson(1) was called
Verify on mock that mock.getPerson(5) was called
Verify times 2 on mock that mock.getPerson(any()) was called
}

@Test
fun failWhenAMethodWasNotCalled() {
val mock = mock(Database::class)
mock.getPerson(1)
Verify on mock that mock.getPerson(1) was called
assertFails { Verify times 1 on mock that mock.getPerson(5) was called }
}

@Test
fun failWhenAMethodWasCalledLessThanSpecified() {
val mock = mock(Database::class)
mock.getPerson(1)
Verify on mock that mock.getPerson(1) was called
assertFails { Verify times 2 on mock that mock.getPerson(any()) was called }
}

@Test
fun failWhenAMethodWasCalledMoreThanSpecified() {
val mock = mock(Database::class)
mock.getPerson(1)
mock.getPerson(5)
Verify on mock that mock.getPerson(1) was called
Verify on mock that mock.getPerson(5) was called
assertFails { Verify times 1 on mock that mock.getPerson(any()) was called }
}
}

0 comments on commit 8f98e79

Please sign in to comment.