From abfa7e2c65b9faf55afa5dc2ed62563ee2264d0a Mon Sep 17 00:00:00 2001 From: "smain@google.com" Date: Wed, 10 Aug 2016 12:59:24 -0700 Subject: [PATCH] add info about how to return default values from android.jar bug: 30757670 Change-Id: I84e1624cbe7ae8d5e4e2886d4e8f61d621729464 --- .../testing/unit-testing/local-unit-tests.jd | 36 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/docs/html/training/testing/unit-testing/local-unit-tests.jd b/docs/html/training/testing/unit-testing/local-unit-tests.jd index 8b109ee36a0e..d19de4f6544a 100644 --- a/docs/html/training/testing/unit-testing/local-unit-tests.jd +++ b/docs/html/training/testing/unit-testing/local-unit-tests.jd @@ -112,12 +112,16 @@ Hamcrest matchers (such as the {@code is()} and {@code equalTo()} methods) t returned result against the expected result.

Mock Android dependencies

-

-By default, the -Android Plug-in for Gradle executes your local unit tests against a modified -version of the {@code android.jar} library, which does not contain any actual code. Instead, method -calls to Android classes from your unit test throw an exception. -

+ +

By default, the Android Plug-in for +Gradle executes your local unit tests against a modified version of the +{@code android.jar} library, which does not contain any actual code. Instead, +method calls to Android classes from your unit test throw an exception. This is +to make sure you test only your code and do not depend on any +particular behavior of the Android platform (that you have not explicitly +mocked).

+

You can use a mocking framework to stub out external dependencies in your code, to easily test that your component interacts with a dependency in an expected way. By substituting Android dependencies @@ -195,6 +199,26 @@ class="external-link">Mockito API reference and the class="external-link">sample code.

+

If the exceptions thrown by Android APIs in the +android.jar are problematic for your tests, you can change the behavior so that methods +instead return either null or zero by adding the following configuration in your project's +top-level build.gradle file:

+ +
+android {
+  ...
+  testOptions {
+    unitTests.returnDefaultValues = true
+  }
+}
+
+ +

Caution: +Setting the returnDefaultValues property to true +should be done with care. The null/zero return values can introduce +regressions in your tests, which are hard to debug and might allow failing tests +to pass. Only use it as a last resort.

+

Run Local Unit Tests

-- 2.11.0