Skip to content

Commit

Permalink
Added Mockito helper functions (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffasitella committed Nov 14, 2022
1 parent 19627d6 commit 0d3c4a4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions testing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ ext {
espresso_version = '3.4.0'
arch_core_testing_version = '2.1.0'
fragment_testing_version = '1.4.0'
mockito_version = '4.0.0'
}

dependencies {
Expand All @@ -108,5 +109,7 @@ dependencies {
api "androidx.test.ext:junit:$android_test_junit_version"
api "androidx.test.espresso:espresso-core:$espresso_version"
api "androidx.test.espresso:espresso-intents:$espresso_version"
api "org.mockito.kotlin:mockito-kotlin:$mockito_version"
api "org.mockito:mockito-inline:$mockito_version"
debugImplementation "androidx.fragment:fragment-testing:$fragment_testing_version"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ar.com.wolox.wolmo.testing.mockito

import org.mockito.Mockito
import org.mockito.Mockito.atLeastOnce
import org.mockito.Mockito.times

/**
* Use this method to verify a certain behavior happened just a single time.
*
* @param mock the mocked class.
* <pre>{@code
* verifyOnce(mockedClass).expectedBehavior()}
* </pre>
*/
fun <T> verifyOnce(mock: T): T =
Mockito.verify(mock, times(1))

/**
* Use this method to verify a certain behavior happened at least one time.
*
* @param mock the mocked class.
* <pre>{@code
* verifyAtLeastOnce(mockedClass).expectedBehavior()}
* </pre>
*/
fun <T> verifyAtLeastOnce(mock: T): T =
Mockito.verify(mock, atLeastOnce())

/**
* Use this method to verify a certain behavior did not happen at all.
*
* @param mock the mocked class.
* <pre>{@code
* verifyZeroTimes(mockedClass).unexpectedBehavior()}
* </pre>
*/
fun <T> verifyZeroTimes(mock: T): T =
Mockito.verify(mock, times(0))

/**
* Use this method to verify a certain behavior happened a custom number of times.
*
* @param mock the mocked class.
* <pre>{@code
* verifyCustomTimes(mockedClass, 3).expectedBehavior()}
* </pre>
*/
fun <T> verifyCustomTimes(mock: T, times: Int) =
Mockito.verify(mock, times(times))

0 comments on commit 0d3c4a4

Please sign in to comment.