Does anyone know a good way to learn unit/instrume...
# android
r
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
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
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
assertEquals do nothing if expected argument equals to actual argument, otherwise throws exception Just tool to simplify test writing
r
I know what it does and I know how to setup the testing I just don't know how to execute them properly
g
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
Let me show you what I have so far
g
./gradlew test
for Unit tests
./gradlew checkConnected
for Instrumentation tests (check on connected device)
Okay, it’s instrumentation test
r
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
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
So, just to clarify. What if I have an android framework method? Do I have to mock things with context for example?
g
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
Got it. Thank you so much. Helped me a lot.
g
btw official introduction to testing fro Android describes this really well https://developer.android.com/training/testing/fundamentals.html
r
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.