| OLD | NEW |
| 1 # JUnit 4 Migration | 1 # JUnit 4 Migration |
| 2 | 2 |
| 3 As of Android 24 (N), JUnit3 style javatests have been deprecated for the new | 3 As of Android 24 (N), JUnit3 style javatests have been deprecated for the new |
| 4 JUnit4-based [Android Testing Support Library][1]. | 4 JUnit4-based [Android Testing Support Library][1]. |
| 5 We are in the process of changing all instrumentation tests in chromium to | 5 We are in the process of changing all instrumentation tests in chromium to |
| 6 JUnit4 style. This doc explains the differences between JUnit3 and JUnit4 | 6 JUnit4 style. This doc explains the differences between JUnit3 and JUnit4 |
| 7 instrumentation tests and how to write or convert them. | 7 instrumentation tests and how to write or convert them. |
| 8 | 8 |
| 9 [TOC] | 9 [TOC] |
| 10 | 10 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // 2: Code here runs before @Before method | 139 // 2: Code here runs before @Before method |
| 140 base.evaluate() | 140 base.evaluate() |
| 141 // 3: Code here runs after @After method | 141 // 3: Code here runs after @After method |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 ``` | 146 ``` |
| 147 | 147 |
| 148 | 148 |
| 149 ## Caveats | 149 ## Common Errors |
| 150 | 150 |
| 151 1. Instrumentation tests that rely on test thread to have message handler | 151 1. Instrumentation tests that rely on test thread to have message handler |
| 152 will not work. For example error message: | 152 will not work. For example error message: |
| 153 | 153 |
| 154 java.lang.RuntimeException: Can't create handler inside thread that has
not called Looper.prepare() | 154 java.lang.RuntimeException: Can't create handler inside thread that has
not called Looper.prepare() |
| 155 | 155 |
| 156 Please utilize `@UiThreadTest` or | 156 or |
| 157 `ActivityTestRule.runOnUiThread(Runnable r)` to refactor these tests. | |
| 158 For more, check this [GitHub issue][6] | |
| 159 | 157 |
| 160 1. Use `@UiThreadTest` with caution. Currently, | 158 java.lang.IllegalStateException: The current thread must have a looper! |
| 161 **@UiThreadTest is only effective when UiThreadTestRule or | 159 |
| 162 ActivityTestRule is declared** in the test class. Please use | 160 Please utilize `ActivityTestRule.runOnUiThread(Runnable r)` to refactor |
| 163 `android.support.test.annotation.UiThreadTest`, not | 161 these tests. For more, check this [GitHub issue][6] |
| 164 `android.test.UiThreadTest`. When using @UiThreadTest, **it would cause | 162 |
| 165 `setUp` and `tearDown` to run in Ui Thread** as well. Avoid that by simply | 163 1. Use `@UiThreadTest` with caution!! |
| 166 calling [`runOnUiThread`][9] or [`runOnMainSync`][10] with a Runnable. | 164 - Currently, **@UiThreadTest is only effective when UiThreadTestRule or |
| 165 ActivityTestRule is declared** in the test class. |
| 166 - Please use **`android.support.test.annotation.UiThreadTest`, NOT |
| 167 `android.test.UiThreadTest`**. |
| 168 - When using @UiThreadTest, **it would cause `setUp` and `tearDown` to |
| 169 run in Ui Thread** as well. Avoid that by calling [`runOnUiThread`][9] |
| 170 or [`runOnMainSync`][10] with a Runnable. |
| 167 | 171 |
| 168 ```java | 172 ```java |
| 169 // Wrong test | 173 // Wrong test |
| 170 public class Test { | 174 public class Test { |
| 171 @Rule | 175 @Rule |
| 172 public ActivityTestRule<MyActivity> mRule = new ActivityTestRule<>( | 176 public ActivityTestRule<MyActivity> mRule = new ActivityTestRule<>( |
| 173 MyActivity.class> | 177 MyActivity.class> |
| 174 | 178 |
| 175 @Before | 179 @Before |
| 176 public void setUp() { | 180 public void setUp() { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 204 mRule.runOnUiThread(new Runnable() { | 208 mRule.runOnUiThread(new Runnable() { |
| 205 @Override | 209 @Override |
| 206 public void run() { | 210 public void run() { |
| 207 actionThatNeedsUiThread(); | 211 actionThatNeedsUiThread(); |
| 208 } | 212 } |
| 209 }); | 213 }); |
| 210 } | 214 } |
| 211 } | 215 } |
| 212 ``` | 216 ``` |
| 213 | 217 |
| 214 | |
| 215 1. `assertEquals(float a, float b)` and `assertEquals(double a, double b)` are | 218 1. `assertEquals(float a, float b)` and `assertEquals(double a, double b)` are |
| 216 deprecated in JUnit4's Assert class. **Despite only generating a warning at | 219 deprecated in JUnit4's Assert class. **Despite only generating a warning at |
| 217 build time, they fail at runtime.** Please use | 220 build time, they fail at runtime.** Please use |
| 218 `Assert.assertEquals(float a, float b, float delta)` | 221 `Assert.assertEquals(float a, float b, float delta)` |
| 219 | 222 |
| 223 1. Errorprone expects all public methods starting with `test...` to be |
| 224 annotated with `@Test`. Failure to meet that expectation will cause |
| 225 errorprone to fail with something like this: |
| 226 |
| 227 [JUnit4TestNotRun] Test method will not be run; please add @Test annotat
ion |
| 228 |
| 229 In particular, you may see this when attempting to disable tests. In that |
| 230 case, the test should be annotated with both @DisabledTest and @Test. |
| 220 | 231 |
| 221 ## Common questions | 232 ## Common questions |
| 222 | 233 |
| 223 - Q: Are `@Test` and `@LargeTest/@MediumTest/@SmallTest` annotation both | 234 - Q: Are `@Test` and `@LargeTest/@MediumTest/@SmallTest` annotation both |
| 224 necessary? | 235 necessary? |
| 225 - A: Yes, both are required for now. We plan to refactor this in the | 236 - A: Yes, both are required for now. We plan to refactor this in the |
| 226 future. | 237 future. |
| 227 - Q: Isn't the inheritance of the Test classes just migrated to inheritance | 238 - Q: Isn't the inheritance of the Test classes just migrated to inheritance |
| 228 of TestRules? | 239 of TestRules? |
| 229 - A: Yes. During the migration, we plan to maintain a 1:1 mapping between | 240 - A: Yes. During the migration, we plan to maintain a 1:1 mapping between |
| (...skipping 15 matching lines...) Expand all Loading... |
| 245 [1]: https://developer.android.com/topic/libraries/testing-support-library/index
.html | 256 [1]: https://developer.android.com/topic/libraries/testing-support-library/index
.html |
| 246 [2]: https://cs.chromium.org/chromium/src/android_webview/tools/system_webview_s
hell/layout_tests/AndroidManifest.xml?l=36 | 257 [2]: https://cs.chromium.org/chromium/src/android_webview/tools/system_webview_s
hell/layout_tests/AndroidManifest.xml?l=36 |
| 247 [3]: http://junit.org/junit4/javadoc/4.12/org/junit/rules/TestRule.html | 258 [3]: http://junit.org/junit4/javadoc/4.12/org/junit/rules/TestRule.html |
| 248 [4]: https://developer.android.com/reference/android/support/test/rule/ActivityT
estRule.html | 259 [4]: https://developer.android.com/reference/android/support/test/rule/ActivityT
estRule.html |
| 249 [5]: https://github.com/yoland68/chromium-junit-auto-migrate | 260 [5]: https://github.com/yoland68/chromium-junit-auto-migrate |
| 250 [6]: http://github.com/skyisle/android-test-kit/issues/121 | 261 [6]: http://github.com/skyisle/android-test-kit/issues/121 |
| 251 [7]: https://bugs.chromium.org/p/chromium/issues/detail?id=640116 | 262 [7]: https://bugs.chromium.org/p/chromium/issues/detail?id=640116 |
| 252 [8]: http://junit.org/junit4/javadoc/4.12/org/junit/rules/RuleChain.html | 263 [8]: http://junit.org/junit4/javadoc/4.12/org/junit/rules/RuleChain.html |
| 253 [9]: https://developer.android.com/reference/android/app/Instrumentation.html#ru
nOnMainSync(java.lang.Runnable) | 264 [9]: https://developer.android.com/reference/android/app/Instrumentation.html#ru
nOnMainSync(java.lang.Runnable) |
| 254 [10]: https://developer.android.com/reference/android/support/test/rule/UiThread
TestRule.html#runOnUiThread(java.lang.Runnable) | 265 [10]: https://developer.android.com/reference/android/support/test/rule/UiThread
TestRule.html#runOnUiThread(java.lang.Runnable) |
| OLD | NEW |