Testing Android blocks to abandon Debug assembly type

My unit tests do not work with the d method in android.util.Log do not scoff, but only when testDebug starts. If testRelease works, everything is fine and they pass correctly. Does anyone know why this is happening? The same thing happens when you start gradle from the console and Android studio.

+5
source share
2 answers

Here is an explanation of how I decided this for future reference. The problem with tests working in debugging but not in release was due to the fact that Log.d (and friends from the android framework) mocked incorrectly. The reason it works when building as an issue is because our login was based on this property from the build configuration. Basically we have if (BuildConfig.type! = "Release") Log.d (...), and since the compiler removes this block due to the final value, it is not called when testing the release. To mock the static Log.d method, I used PowerMock. Focusing was easy, but setting up a Power Mock is really a hassle, so there may perhaps be ways to do it.

+1
source

Place an order "Method ... do not scoff" on the Android Studio Project website. It says:

The android.jar file, which is used to run unit tests, does not contain any real code that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default). This is done in order to make sure that your module only tests your code and is not dependent on any specific behavior of the Android platform (that you obviously did not scoff, for example, using Mockito). If this proves problematic, you can add the snippet below to your build.gradle to change this behavior:

build.gradle android { // ... testOptions { unitTests.returnDefaultValues ​​= true } } 

We know that the default behavior is problematic when using classes such as Log or TextUtils and evaluates possible solutions in future releases.

I just used the above to get rid of the exception at the moment.

0
source

All Articles