Local JVM unit tests

Refreshing my knowlegde on local unit tests was quite fun. I’d definitely recommend this codelab to brush up your skills.

I have uploaded a code sample on my Github that demonstrates different ways in which you can write JVM tests.

Testing standalone POJO’s

Tests that run on the JVM and has nothing to do with Android. Its purpose is to test the logic used within in the Application like testing the SimpleCalc (in the Codelabs) or testing FizzBuzz (my GitHub).

Testing Views?

JVM tests use a modified version of the android.jar file. Therefore, it’s impossible to test views however you can test an interface that the view implements. This allows you to test what’s important and not worry about Android things like reading text, setting the background colour or displaying information to the user. I would highly recommend using Mockito so that you don’t have to mock your views manually.

Robolectric to the rescue

Well, it is not entirely impossible to unit views. Robolectric is impressive, it allows for unit tests that involve the Android framework, and you are not tied down to using MVP for view testing. Robolectric defangs the Android SDK by rewriting the Android classes as they are loaded. However, it would be best if you prepared yourself as the setup could involve a lot of troubleshooting.