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. | 157 java.lang.IllegalStateException: The current thread must have a looper! |
jbudorick
2017/05/04 19:40:47
I think you need an extra blank line before this o
the real yoland
2017/05/04 19:53:28
Done
| |
158 For more, check this [GitHub issue][6] | |
159 | 158 |
160 1. Use `@UiThreadTest` with caution. Currently, | 159 Please utilize `ActivityTestRule.runOnUiThread(Runnable r)` to refactor |
161 **@UiThreadTest is only effective when UiThreadTestRule or | 160 these tests. For more, check this [GitHub issue][6] |
162 ActivityTestRule is declared** in the test class. Please use | 161 |
163 `android.support.test.annotation.UiThreadTest`, not | 162 1. Use `@UiThreadTest` with caution!! |
164 `android.test.UiThreadTest`. When using @UiThreadTest, **it would cause | 163 - Currently, **@UiThreadTest is only effective when UiThreadTestRule or |
165 `setUp` and `tearDown` to run in Ui Thread** as well. Avoid that by simply | 164 ActivityTestRule is declared** in the test class. |
166 calling [`runOnUiThread`][9] or [`runOnMainSync`][10] with a Runnable. | 165 - Please use **`android.support.test.annotation.UiThreadTest`, NOT |
166 `android.test.UiThreadTest`**. | |
167 - When using @UiThreadTest, **it would cause `setUp` and `tearDown` to | |
168 run in Ui Thread** as well. Avoid that by calling [`runOnUiThread`][9] | |
169 or [`runOnMainSync`][10] with a Runnable. | |
167 | 170 |
168 ```java | 171 ```java |
169 // Wrong test | 172 // Wrong test |
170 public class Test { | 173 public class Test { |
171 @Rule | 174 @Rule |
172 public ActivityTestRule<MyActivity> mRule = new ActivityTestRule<>( | 175 public ActivityTestRule<MyActivity> mRule = new ActivityTestRule<>( |
173 MyActivity.class> | 176 MyActivity.class> |
174 | 177 |
175 @Before | 178 @Before |
176 public void setUp() { | 179 public void setUp() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
211 } | 214 } |
212 ``` | 215 ``` |
213 | 216 |
214 | 217 |
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 |
220 | 223 |
224 1. Error-prone warning when disabling a javatest in JUnit4. If you get a | |
jbudorick
2017/05/04 19:40:47
Rephrase:
1. Errorprone expects all methods star
the real yoland
2017/05/04 19:53:28
Done
| |
225 `[JUnit4TestNotRun] Test method will not be run; please add @Test annotation ` | |
226 warning. It's likely because you commented out `@Test` when adding | |
227 `@DisabledTest` or `@FlakyTest`. | |
228 | |
221 ## Common questions | 229 ## Common questions |
222 | 230 |
223 - Q: Are `@Test` and `@LargeTest/@MediumTest/@SmallTest` annotation both | 231 - Q: Are `@Test` and `@LargeTest/@MediumTest/@SmallTest` annotation both |
224 necessary? | 232 necessary? |
225 - A: Yes, both are required for now. We plan to refactor this in the | 233 - A: Yes, both are required for now. We plan to refactor this in the |
226 future. | 234 future. |
227 - Q: Isn't the inheritance of the Test classes just migrated to inheritance | 235 - Q: Isn't the inheritance of the Test classes just migrated to inheritance |
228 of TestRules? | 236 of TestRules? |
229 - A: Yes. During the migration, we plan to maintain a 1:1 mapping between | 237 - A: Yes. During the migration, we plan to maintain a 1:1 mapping between |
230 the test base classes and TestRules (e.g. ContentShellTestBase to | 238 the test base classes and TestRules (e.g. ContentShellTestBase to |
(...skipping 14 matching lines...) Expand all Loading... | |
245 [1]: https://developer.android.com/topic/libraries/testing-support-library/index .html | 253 [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 | 254 [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 | 255 [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 | 256 [4]: https://developer.android.com/reference/android/support/test/rule/ActivityT estRule.html |
249 [5]: https://github.com/yoland68/chromium-junit-auto-migrate | 257 [5]: https://github.com/yoland68/chromium-junit-auto-migrate |
250 [6]: http://github.com/skyisle/android-test-kit/issues/121 | 258 [6]: http://github.com/skyisle/android-test-kit/issues/121 |
251 [7]: https://bugs.chromium.org/p/chromium/issues/detail?id=640116 | 259 [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 | 260 [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) | 261 [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) | 262 [10]: https://developer.android.com/reference/android/support/test/rule/UiThread TestRule.html#runOnUiThread(java.lang.Runnable) |
OLD | NEW |