0

I want to verify that someMethod is being called once when I call doSomething(). Within doSomething() the currentTimeMillis() is being called and this value will not be exactly the same. How can I test this?

Currently I get this error message:

*org.mockito.exceptions.misusing.UnfinishedVerificationException:

Missing method call for verify(mock) here: -> at myapplication.SomeLoginClassTest.trackDoSomething handles values(SomeLoginClassTest.kt:24)

Example of correct verification: verify(mock).doSomething()*

class SomeLoginClassTest {

    @get:Rule
    val mockitoRule = MockitoJUnit.rule()

    @Mock
    lateinit var someClass: SomeClass

    @InjectMocks
    lateinit var someLoginClass: SomeLoginClass

    @Test
    fun `trackDoSomething handles values`() {
        someLoginClass.doSomething()
        Mockito.verify(someClass).someMethod(System.currentTimeMillis())
    }
}

    open class SomeLoginClass @Inject constructor(private val someClass: SomeClass) {

    @RequiresApi(Build.VERSION_CODES.N)
    fun doSomething() {
        val elapsedTime = System.currentTimeMillis()
        someClass.someMethod(elapsedTime)
    }
}

open class SomeClass {

    @RequiresApi(Build.VERSION_CODES.N)
    public fun someMethod(elapsedTime: Long) {
        var elapsed = Random().longs()
    }
}

Build.gradle:

// Dagger
api "com.google.dagger:dagger:2.13"
kapt "com.google.dagger:dagger-compiler:2.13"

api "com.google.dagger:dagger-android-support:2.13"
kapt "com.google.dagger:dagger-android-processor:2.13"

androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
testCompile 'org.mockito:mockito-core:2.18.3'
testCompile 'junit:junit:4.12'

// Dependencies for Android unit tests
testImplementation "org.mockito:mockito-core:2.18.3"
androidTestImplementation "org.mockito:mockito-android:2.18.3"
androidTestImplementation "com.nhaarman:mockito-kotlin:1.5.0"

I've tried this:

Missing method call for verify(mock), but there is one?

Mockito gives UnfinishedVerificationException when it seems OK

Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • Instead of specifying the exact long value, try using anyLong(). If you care about the value, then you need to wrap currentTimeMillis in some other class you can control. – AutonomousApps Jul 05 '18 at 06:20
  • I don't care and I get the error: UnfinishedVerificationException. Missing method call for verify(mock) here. – Jim Clermonts Jul 09 '18 at 07:58

1 Answers1

0

Mockito.verify(someClass).someMethod(System.currentTimeMillis())

is the incorrect syntax for verification it should be

Mockito.verify(someClass).someMethod(Mockito.eq(System.currentTimeMillis()))

and as the comment that AutonomousApps suggested it should be

Mockito.verify(someClass).someMethod(Mockito.anyLong())

ndrone
  • 3,524
  • 2
  • 23
  • 37