当我调用doSomething()时,我想验证一次someMethod是否被调用。在doSomething()中,将调用currentTimeMillis(),并且该值将不完全相同。我该如何测试?
当前我收到此错误消息:
* org.mockito.exceptions.misusing.UnfinishedVerificationException:
在此处缺少对verify(mock)的方法调用: ->在myapplication.SomeLoginClassTest.trackDoSomething处理值(SomeLoginClassTest.kt:24)
正确验证的示例: 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"
我已经尝试过了:
Missing method call for verify(mock), but there is one?
Mockito gives UnfinishedVerificationException when it seems OK
答案 0 :(得分:0)
Mockito.verify(someClass).someMethod(System.currentTimeMillis())
是用于验证的错误语法
Mockito.verify(someClass).someMethod(Mockito.eq(System.currentTimeMillis()))
AutonomousApps提出的评论应该是
Mockito.verify(someClass).someMethod(Mockito.anyLong())