https://kotlinlang.org logo
#android
Title
# android
k

Kulwinder Singh

03/09/2020, 7:58 AM
How can we mock
FirebaseAuth.getInstance().signInWithCustomToken(token).addOnCompleteListener{}
and other Task like
addOnSuccessListener
and
addOnFailureListener
using MockK ?
s

Samme Kleijn

03/09/2020, 8:09 AM
I used to do a similar thing like this:
Copy code
@MockK(relaxed = true)
private lateinit var mockTask: Task<List<FirebaseVisionBarcode>>
Copy code
every { mockTask.addOnFailureListener(any()) } answers {
   firstArg<OnFailureListener>().onFailure(mockk())
   mockTask
}
To mock invoke a failure
👍 1
m

Matej Drobnič

03/09/2020, 8:15 AM
Don't mock tasks
return real tasks, for example
Tasks.forException(...)
👆 1
k

Kulwinder Singh

03/09/2020, 8:21 AM
for
FirebaseAuth.getInstance().signInWithCustomToken(token).addOnCompleteListener
i have created
Copy code
lateinit var mockAuthTask: Task<AuthResult>
...
beforeEachTest{
.....
every { mockAuthTask.addOnSuccessListener(any()) } answers { firstArg<OnCompleteListener<AuthResult>>().onComplete(mockk()) }
}
but its not working how can i do it @Samme Kleijn
@Matej Drobnič can you please give me more info
?
m

Matej Drobnič

03/09/2020, 8:38 AM
instead of whole mock stuff, just create dummy AuthResult and create real task:
Copy code
val result = AuthResult(...)
val authTask = Tasks.forResult(result)
k

Kulwinder Singh

03/09/2020, 10:06 AM
@Matej Drobnič but AuthResult is interface i don't know how to implement that class as Concrete
for this do i also have to update real code(UseCases,Repositories) or just tests? @Matej Drobnič, because i'm not in situation to update real UseCases or repositories i am just fixing mocking issues
actually i have tried as @Samme Kleijn suggested, but i'm getting this
Failure(e=io.mockk.MockKException: no answer found for: Task(#17).getException())
i think i'm getting this because we are sending
AuthResult
as mockobject but now i don't know how to solve it 😞
m

Matej Drobnič

03/09/2020, 10:48 AM
just tests
you can use
mockk()
instead of implementing AuthResult I guess
or you can create your own
FakeAuthResult
class that implements the interface
k

Kulwinder Singh

03/09/2020, 10:53 AM
@Matej Drobnič actually
mock(relaxed=true)
worked
but this is mocked, but as you said don't mock tasks and return real, what is Tasks.forExceptions() and how can i return real tasks, is it going to connect to connect Firebase server and return real tasks ? @Matej Drobnič
m

Matej Drobnič

03/09/2020, 11:15 AM
Task is just a wraper that can return either result or exception
it will not go to firebase
Tasks.forResult(mockk())
is a good compromise
you don't mock the task, but you mock real value
but it would be even better if you don't mock anything and just return concrete AuthResult
👍 1
k

Kulwinder Singh

03/09/2020, 11:28 AM
Thanks @Matej Drobnič & @Samme Kleijn, both of yours solutions really helped me.
👍 1
after working everything currently i'm getting
Copy code
no answer found for: SomeClass(#3).setSomeVariable(dummy)
how can i add
every
for setters of fields ?
l

luke

03/24/2020, 3:21 AM
240 Views