DEV Community

Discussion on: Android testing help

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard • Edited

@fultonbrowne Sorry for the bad news, but testing on Android is hard

Part of the answer is that the mockK library allows you to mock things

GitHub logo mockk / mockk

mocking library for Kotlin

But the real problem is that the Android SDK was never designed to allows you to unit test easily. So you have the so-called Burito Design Pattern where you business logic is inside Activity or Fragment

And basically as soon as your business logic lives inside an Android component, you are basically fucked up for writing unit tests.

Google pushes you instead to write UX tests, which also sucks because they are hard to write, slow and flaky and usually tell you little when they fail.

Google Testing Blog: Just say NO to more end to end tests

So the real solution in my opinion is to extract your business logic outside of Android components

To this I use the Functional Core - Imperative Shell approach

More links here
gist.github.com/kbilsted/abdc01785...

Collapse
 
fultonbrowne profile image
Fulton Browne

thanks for the advice! your post is going to be very usefully :}

Collapse
 
corentinleffy profile image
Corentin Leffy • Edited

Jean-Michel is right : Android Test is difficult and you have to extract your business logic outside of Android to not be too coupled with the framework.

To complete, I suggest you to check this Google Codelabs : it covers the basics of running and writing tests for Android. You can do the next two Codelabs after that to complete what you learned in the first one (this is all about testing, DI, mock, ...).

Thread Thread
 
fultonbrowne profile image
Fulton Browne

Thanks I have extracted as much logic outside the activity as possible but one of the main components of my app NEEDS activity context. completely testing this part will get me to around 65% code coverage (and my project is BIG). I cant find a way to pass the an activity to the method, any tips?