- Create a new test class:
class MyTest {
}
- Add a mocked Api type test-class property:
class MyTest {
val api: Api = mock()
}
- Instantiate the Authenticator class as the class property:
class MyTest {
val api: Api = mock()
val authenticator = Authenticator(api)
}
- 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())
}
}
}