My Android application uses AWS Cognito and Amplify Auth SDK for authentication and I'm trying to write JUnit test cases for login/signup flows. I'm using Mockito framework to mock the classes.
I started with login, my login model looks like this
class LoginService(val auth: AuthCategory) {
fun login(username: String, password: String): MutableLiveData<Login> {
val liveData = MutableLiveData<Login>()
auth.signIn(username, password,
{ result ->
liveData.postValue(Login(result, null))
},
{ error ->
liveData.postValue(Login(null, error))
}
)
return liveData
}
}
And my viewmodel calls it this way
class LoginViewModel : ViewModel() {
val loginService = LoginService(Amplify.Auth)
fun login(username: String, password: String): MutableLiveData<Login> {
return loginService.login(username, password)
}
}
And my test case looks like this
lateinit var auth: AuthCategory
lateinit var loginService: LoginService
@Before
fun onSetup() {
auth = mock(Amplify.Auth::class.java)
loginService = LoginService(auth)
}
@Test
fun loginTest() {
val authSignIn: Consumer<*>? = mock(Consumer::class.java)
val authEx: Consumer<*> = mock(Consumer::class.java)
`when`(
auth.signIn(
anyString(), anyString(),
authSignIn as Consumer<AuthSignInResult>, authEx as Consumer<AuthException>
)
)
loginService.login("username", "password").observeForever {
assertTrue(it.result?.isSignInComplete!!)
}
}
Please help me validate this approach,
I'm trying to find out a way to trigger AuthSignInResult
and AuthException
of Auth.signIn()
method so that I would assert if signin is successful or there is an error.
I'm very new to AWS Amplify and Cognito environment, A suggestion/reference to do this in correct way would be highly appreciated. Thanks in advance.