I believe there is some big gap in my understanding about RxJava
suppose we have this interface:
interface SecretInterface {
fun getSecretKey(): Single<Int>
}
interface Vault {
fun openSecret(): Single<String>
}
and then I have this test:
@Test
fun should_check_on_this() {
val mockSecret = mock<SecretInterface> {
on { this.getSecretKey() }.thenReturn(Single.just(1), Single.just(2),
Single.just(3))
}
val mockVault = mock<Vault> {
on { this.openSecret() }.thenReturn(Single.error(Exception("no can do")),
Single.just("secret opened"))
}
val complexStuff = mockSecret.getSecretKey().doOnSuccess {
System.err.println("onSuccess secret top $it")
}.flatMap {
mockVault.openSecret()
}.retryWhen {
it.flatMapSingle {
mockSecret.getSecretKey().doOnSuccess { System.err.println("onSuccess secret bottom: $it") }
}
}
val test = complexStuff.test()
test.assertValue { it == "secret opened" }
verify(mockSecret, Times(3)).getSecretKey()
}
the test failed, because mockSecret.getSecretKey() invocated only twice, but in output:
onSuccess secret top 1
onSuccess secret bottom: 2
onSuccess secret top 1
org.mockito.exceptions.verification.TooFewActualInvocations:
secretInterface.getSecretKey();
Wanted 3 times:
things that I can not understand is:
-It's already resubscribed, but how come still using old value?
-How I can make it, that every time I resubscribed always return the latest value?
in this case it should return 3 not 1.
thank you