| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 1 | page.title=Building Local Unit Tests |
| 2 | page.tags=testing,androidjunitrunner,junit,unit test,mock |
| 3 | trainingnavtop=true |
| 4 | |
| 5 | @jd:body |
| 6 | |
| 7 | <!-- This is the training bar --> |
| 8 | <div id="tb-wrapper"> |
| 9 | <div id="tb"> |
| 10 | <h2>Dependencies and Prerequisites</h2> |
| 11 | |
| 12 | <ul> |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 13 | <li><a href="{@docRoot}tools/studio/index.html">Android Studio (latest version)</a>.</li> |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 14 | </ul> |
| 15 | |
| 16 | <h2>This lesson teaches you to</h2> |
| 17 | |
| 18 | <ol> |
| 19 | <li><a href="#setup">Set Up Your Testing Environment</a></li> |
| 20 | <li><a href="#build">Create a Local Unit Test Class</a></li> |
| 21 | <li><a href="#run">Run Local Unit Tests</a></li> |
| 22 | </ol> |
| 23 | |
| 24 | <h2>Try it out</h2> |
| 25 | |
| 26 | <ul> |
| 27 | <li> |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 28 | <a href="https://github.com/googlesamples/android-testing/tree/master/unit/BasicSample" |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 29 | class="external-link">Local Unit Tests Code Samples</a></li> |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 30 | <li><a href="https://codelabs.developers.google.com/codelabs/android-testing/index.html?index=..%2F..%2Findex#0" |
| 31 | class="external-link">Android Testing Codelab</a></li> |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 32 | </ul> |
| 33 | </div> |
| 34 | </div> |
| 35 | |
| 36 | <p>If your unit test has no dependencies or only has simple dependencies on Android, you should run |
| 37 | your test on a local development machine. This testing approach is efficient because it helps |
| 38 | you avoid the overhead of loading the target app and unit test code onto a physical device or |
| 39 | emulator every time your test is run. Consequently, the execution time for running your unit |
| 40 | test is greatly reduced. With this approach, you normally use a mocking framework, like |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 41 | <a href="https://github.com/mockito/mockito" class="external-link">Mockito</a>, to fulfill any |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 42 | dependency relationships.</p> |
| 43 | |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 44 | <h2 id="setup">Set Up Your Testing Environment</h2> |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 45 | <p>Before building your local unit test, make sure to configure your test source code location and |
| 46 | project dependencies, as described in |
| 47 | <a href="{@docRoot}training/testing/start/index.html#config-local-tests"> |
| 48 | Getting Started with Testing</a>.</p> |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 49 | |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 50 | |
| 51 | <h2 id="build">Create a Local Unit Test Class</h2> |
| 52 | <p>Your local unit test class should be written as a JUnit 4 test class. |
| 53 | <a href="http://junit.org/" class="external-link">JUnit</a> is the most popular |
| 54 | and widely-used unit testing framework for Java. The latest version of this framework, JUnit 4, |
| 55 | allows you to write tests in a cleaner and more flexible way than its predecessor versions. Unlike |
| 56 | the previous approach to Android unit testing based on JUnit 3, with JUnit 4, you do not need to |
| 57 | extend the {@code junit.framework.TestCase} class. You also do not need to prefix your test method |
| 58 | name with the {@code ‘test’} keyword, or use any classes in the {@code junit.framework} or |
| 59 | {@code junit.extensions} package.</p> |
| 60 | |
| 61 | <p>To create a basic JUnit 4 test class, create a Java class that contains one or more test methods. |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 62 | A test method begins with the {@code @Test} annotation and contains the code to exercise |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 63 | and verify a single functionality in the component that you want to test.</p> |
| 64 | |
| 65 | <p>The following example shows how you might implement a local unit test class. The test method |
| 66 | {@code emailValidator_CorrectEmailSimple_ReturnsTrue} verifies that the {@code isValidEmail()} |
| 67 | method in the app under test returns the correct result.</p> |
| 68 | |
| 69 | <pre> |
| 70 | import org.junit.Test; |
| 71 | import java.util.regex.Pattern; |
| 72 | import static org.junit.Assert.assertFalse; |
| 73 | import static org.junit.Assert.assertTrue; |
| 74 | |
| 75 | public class EmailValidatorTest { |
| 76 | |
| 77 | @Test |
| 78 | public void emailValidator_CorrectEmailSimple_ReturnsTrue() { |
| 79 | assertThat(EmailValidator.isValidEmail("name@email.com"), is(true)); |
| 80 | } |
| 81 | ... |
| 82 | } |
| 83 | </pre> |
| 84 | |
| 85 | <p>To test that components in your app return the expected results, use the |
| 86 | <a href="http://junit.org/javadoc/latest/org/junit/Assert.html" class="external-link"> |
| 87 | junit.Assert</a> methods to perform validation checks (or <em>assertions</em>) to compare the state |
| 88 | of the component under test against some expected value. To make tests more readable, you |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 89 | can use <a href="https://github.com/hamcrest" class="external-link"> |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 90 | Hamcrest matchers</a> (such as the {@code is()} and {@code equalTo()} methods) to match the |
| 91 | returned result against the expected result.</p> |
| 92 | |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 93 | <h3 id="mocking-dependencies">Mocking Android dependencies</h3> |
| 94 | <p> |
| 95 | By default, the <a href="{@docRoot}tools/building/plugin-for-gradle.html"> |
| 96 | Android Plug-in for Gradle</a> executes your local unit tests against a modified |
| 97 | version of the {@code android.jar} library, which does not contain any actual code. Instead, method |
| 98 | calls to Android classes from your unit test throw an exception. |
| 99 | </p> |
| 100 | <p> |
| 101 | You can use a mocking framework to stub out external dependencies in your code, to easily test that |
| 102 | your component interacts with a dependency in an expected way. By substituting Android dependencies |
| 103 | with mock objects, you can isolate your unit test from the rest of the Android system while |
| 104 | verifying that the correct methods in those dependencies are called. The |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 105 | <a href="https://github.com/mockito/mockito" class="external-link">Mockito</a> mocking framework |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 106 | for Java (version 1.9.5 and higher) offers compatibility with Android unit testing. |
| 107 | With Mockito, you can configure mock objects to return some specific value when invoked.</p> |
| 108 | |
| 109 | <p>To add a mock object to your local unit test using this framework, follow this programming model: |
| 110 | </p> |
| 111 | |
| 112 | <ol> |
| 113 | <li> |
| 114 | Include the Mockito library dependency in your {@code build.gradle} file, as described in |
| 115 | <a href="#setup">Set Up Your Testing Environment</a>. |
| 116 | </li> |
| 117 | <li>At the beginning of your unit test class definition, add the |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 118 | {@code @RunWith(MockitoJUnitRunner.class)} annotation. This annotation tells the Mockito test |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 119 | runner to validate that your usage of the framework is correct and simplifies the initialization of |
| 120 | your mock objects. |
| 121 | </li> |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 122 | <li>To create a mock object for an Android dependency, add the {@code @Mock} annotation before |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 123 | the field declaration.</li> |
| 124 | <li>To stub the behavior of the dependency, you can specify a condition and return |
| 125 | value when the condition is met by using the {@code when()} and {@code thenReturn()} methods. |
| 126 | </li> |
| 127 | </ol> |
| 128 | |
| 129 | <p> |
| 130 | The following example shows how you might create a unit test that uses a mock |
| 131 | {@link android.content.Context} object. |
| 132 | </p> |
| 133 | |
| 134 | <pre> |
| 135 | import static org.hamcrest.MatcherAssert.assertThat; |
| 136 | import static org.hamcrest.CoreMatchers.*; |
| 137 | import static org.mockito.Mockito.*; |
| 138 | import org.junit.Test; |
| 139 | import org.junit.runner.RunWith; |
| 140 | import org.mockito.Mock; |
| 141 | import org.mockito.runners.MockitoJUnitRunner; |
| 142 | import android.content.SharedPreferences; |
| 143 | |
| 144 | @RunWith(MockitoJUnitRunner.class) |
| 145 | public class UnitTestSample { |
| 146 | |
| 147 | private static final String FAKE_STRING = "HELLO WORLD"; |
| 148 | |
| 149 | @Mock |
| 150 | Context mMockContext; |
| 151 | |
| 152 | @Test |
| 153 | public void readStringFromContext_LocalizedString() { |
| 154 | // Given a mocked Context injected into the object under test... |
| 155 | when(mMockContext.getString(R.string.hello_word)) |
| 156 | .thenReturn(FAKE_STRING); |
| 157 | ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext); |
| 158 | |
| 159 | // ...when the string is returned from the object under test... |
| 160 | String result = myObjectUnderTest.getHelloWorldString(); |
| 161 | |
| 162 | // ...then the result should be the expected one. |
| 163 | assertThat(result, is(FAKE_STRING)); |
| 164 | } |
| 165 | } |
| 166 | </pre> |
| 167 | |
| 168 | <p> |
| 169 | To learn more about using the Mockito framework, see the |
| 170 | <a href="http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html" |
| 171 | class="external-link">Mockito API reference</a> and the |
| 172 | {@code SharedPreferencesHelperTest} class in the |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 173 | <a href="https://github.com/googlesamples/android-testing/tree/master/unit/BasicSample" |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 174 | class="external-link">sample code</a>. |
| 175 | </p> |
| 176 | |
| 177 | <h2 id="run">Run Local Unit Tests</h2> |
| 178 | <p> |
| Trevor Johns | 682c24e | 2016-04-12 10:13:47 -0700 | [diff] [blame] | 179 | To run your tests, follow the steps for running local unit tests |
| 180 | described in <a href="{@docRoot}training/testing/start/index.html#run-local-tests"> |
| 181 | Getting Started with Testing</a>. |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 182 | </p> |
| Quddus Chong | 4bc762a | 2015-04-07 16:42:24 -0700 | [diff] [blame] | 183 | |