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

Rick

01/30/2018, 4:39 AM
Does anyone know a good way to learn unit/instrumental testing? I am having a hard time trying to learn this. Second day studying and can't understand. It looks like a new language that I have to memorize. Could'nt find a good book or article that have this subject well explained. Any help/recomendation here?
g

gildor

01/30/2018, 4:43 AM
There is a lot of articles that covers how to configure unit and instrumentation for Android, but as I understand you want some more general explanation. What exactly you don’t understand?
r

Rick

01/30/2018, 4:49 AM
Actually I would like a fast and practical article that go straight to the point and tell me like "Hey, that's what assertEquals does". What I am finding so far is everyone telling why testing is so good, how to design your code to MVP, MVVM, long theorical explanation about things that don't take you anywhere...
Do you know any article that would go straight to the point and explain exactly what needs to be done to make it work in a easy way?\
g

gildor

01/30/2018, 4:50 AM
assertEquals do nothing if expected argument equals to actual argument, otherwise throws exception Just tool to simplify test writing
r

Rick

01/30/2018, 4:52 AM
I know what it does and I know how to setup the testing I just don't know how to execute them properly
g

gildor

01/30/2018, 4:52 AM
Just try to understand what do you want to do in your tests. You probably want to check that output of some code under the tests returns correct values, to do that you just pass known arguments and compare output with known answer. And this the is case where different
assert*
methods are handy
How to execute? Android Project?
r

Rick

01/30/2018, 4:53 AM
Let me show you what I have so far
g

gildor

01/30/2018, 4:53 AM
./gradlew test
for Unit tests
./gradlew checkConnected
for Instrumentation tests (check on connected device)
Okay, it’s instrumentation test
r

Rick

01/30/2018, 4:54 AM
I would like to check if either the toast appear or if intent is triggered (both would indicate the code is running good). This is a registration form.
It is in this case.
How should I handle the testing? Should I test everysingle method?
In unit testing?
g

gildor

01/30/2018, 4:55 AM
There are some ways to do that for toast, just google “check toast espresso”. but better to use mocked toast. For intents there is specially library as part of Espresso https://developer.android.com/training/testing/espresso/intents.html
In best case of course, every public method
r

Rick

01/30/2018, 4:59 AM
So, just to clarify. What if I have an android framework method? Do I have to mock things with context for example?
g

gildor

01/30/2018, 5:00 AM
Better to avoid context and any other Android framework code as much as possible to easily test this code in unit tests. Sometimes just mock context, but it can be easily become really messy, when you mock too much, sometimes better to write some abstraction above
r

Rick

01/30/2018, 5:01 AM
Got it. Thank you so much. Helped me a lot.
g

gildor

01/30/2018, 5:01 AM
btw official introduction to testing fro Android describes this really well https://developer.android.com/training/testing/fundamentals.html
r

Rick

01/30/2018, 6:51 AM
Thank you. Maybe I am a little overwhelmed. I just started 3 months ago with android and I am studying at least 10 hours a day. Maybe I should take a rest and try to study this again and it will be clear. Thank you so much again. I have read this link you sent me but I will read again as soon as I feel good enough to continue.
Hey @gildor I thik I am finally getting the grasp of it. I have two problems. On my presenter constructor I am passing instance of Firebase Database and Authentication. I couldn't figure out a way to make the test work without have to comment there http://prntscr.com/i7tjtd
I tried to mock these instances with mockito and pas on @Before but didn't work.
and how can I test this method? Is there any way I can improve the testing design to make it look/perform better? http://prntscr.com/i7tkce
Thank you.