diff options
| author | 2016-08-11 16:49:59 +0000 | |
|---|---|---|
| committer | 2016-08-11 16:49:59 +0000 | |
| commit | bd2f5388c43296b42aed7589c1353d9adf52b688 (patch) | |
| tree | 7eb8b36e8c9fd62985ed6265b117da65f9b4f981 | |
| parent | 059e260ea3d8551f84462ddf7e38f92cbbbadcc4 (diff) | |
| parent | 93d8ffc4f3ac237e9390a8328699c8cddfad970a (diff) | |
add info about how to return default values from android.jar bug: 30757670 am: abfa7e2c65
am: 93d8ffc4f3
Change-Id: Id4301a4fb462e34428912c12ec570ddfa9010fb2
| -rw-r--r-- | docs/html/training/testing/unit-testing/local-unit-tests.jd | 36 |
1 files 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</a> (such as the {@code is()} and {@code equalTo()} methods) t returned result against the expected result.</p> <h3 id="mocking-dependencies">Mock Android dependencies</h3> -<p> -By default, the <a href="{@docRoot}tools/building/plugin-for-gradle.html"> -Android Plug-in for Gradle</a> 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. -</p> + +<p>By default, the <a href= +"{@docRoot}tools/building/plugin-for-gradle.html">Android Plug-in for +Gradle</a> 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).</p> + <p> 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</a> and the class="external-link">sample code</a>. </p> +<p>If the exceptions thrown by Android APIs in the +<code>android.jar</code> 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 <code>build.gradle</code> file:</p> + +<pre> +android { + ... + testOptions { + unitTests.returnDefaultValues = true + } +} +</pre> + +<p class="caution"><strong>Caution:</strong> +Setting the <code>returnDefaultValues</code> property to <code>true</code> +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.</p> + <h2 id="run">Run Local Unit Tests</h2> |