How to do it...

  1. Create a new test class:
class MyTest {
}

  1. Add a mocked Api type test-class property:
class MyTest {
val api: Api = mock()
}
  1. Instantiate the Authenticator class as the class property:
class MyTest {
val api: Api = mock()
val authenticator = Authenticator(api)
}
  1. Implement the test to verify whether the Api.authorise() function is called at least 10 times in case of consecutive failed authorization attempts:
class MyTest {
val api: Api = mock()
val authenticator = Authenticator(api)

@Test
fun `should retry auth at least 10 times when Api returns empty
token`() {

whenever(api.authorise(any())) doReturn ""

val context = TestCoroutineContext()


runBlocking(context) {
authenticator.tryToAuthorise("admin:1234".toByteArray(),
context)

.await()
context.advanceTimeBy(100, TimeUnit.SECONDS)
verify(api, atLeast(10)).authorise(any())
}
}

}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.180.70