Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Unit Tests failing for Debug build type

My Unit tests are failing with Method d in android.util.Log not mocked but only when I run testDebug. If running testRelease all is fine and they correctly pass. Does anyone know why this is happening? Same thing happens when running gradle from console and Android studio.

like image 955
Igor Čordaš Avatar asked Sep 18 '15 17:09

Igor Čordaš


People also ask

Which Java framework is used to write unit tests in android?

Junit: It is a “Unit Testing” framework for Java Applications. It is an automation framework for Unit as well as UI Testing.

What is unit test in Android?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.


1 Answers

Here is an explanation how I solved this for future reference. Problem with tests working in debug but not release were due to a fact that Log.d (and friends from android framework) were not correctly mocked. Reason for it working when built as release is that our loging was conditional based on this property from build config. Basically we have if (BuildConfig.type!="Release") Log.d (...) and since the compiler removes this block due to final value it doesn't get called when testing release. To mock static method Log.d I used PowerMock. Mocking was easy but setting up Power Mock is really a hassle so there might probably be beter ways to do it.

like image 163
Igor Čordaš Avatar answered Sep 20 '22 04:09

Igor Čordaš