OLD | NEW |
1 # JUnit Tests | 1 # JUnit Tests |
2 | 2 |
3 JUnit tests are Java unit tests. These tests run locally on your workstation. | 3 JUnit tests are Java unit tests. These tests run locally on your workstation. |
4 | 4 |
5 [TOC] | 5 [TOC] |
6 | 6 |
7 ## Writing a JUnit test | 7 ## Writing a JUnit test |
8 | 8 |
9 When writing JUnit tests, you must decide whether you need to use Android code. | 9 When writing JUnit tests, you must decide whether you need to use Android code. |
10 If you want to use Android code you must write a [Robolectric](http://robolectri
c.org/) test. | 10 If you want to use Android code you must write a [Robolectric](http://robolectri
c.org/) test. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 ### JUnit tests with Robolectric | 46 ### JUnit tests with Robolectric |
47 | 47 |
48 Build these types of test using the `junit_binary` GN template. | 48 Build these types of test using the `junit_binary` GN template. |
49 | 49 |
50 Robolectric is a unit testing framework that lets you run tests with Android | 50 Robolectric is a unit testing framework that lets you run tests with Android |
51 code on your workstation. It does this by providing a special version of the | 51 code on your workstation. It does this by providing a special version of the |
52 Android SDK jar that can run in your host JVM. Some more information about | 52 Android SDK jar that can run in your host JVM. Some more information about |
53 Robolectric can be found [here](http://robolectric.org/). | 53 Robolectric can be found [here](http://robolectric.org/). |
54 | 54 |
| 55 One on the main benefits of using Robolectric framework are [shadow classes](htt
p://robolectric.org/extending/). |
| 56 Robolectric comes with many prebuilt shadow classes and also lets you define |
| 57 your own. Whenever an object is instantiated within a Robolectric test, |
| 58 Robolectric looks for a corresponding shadow class (marked by |
| 59 `@Implements(ClassBeingShadowed.class)`). If found, any time a method is invoked |
| 60 on the object, the shadow class's implementation of the method is invoked first. |
| 61 This works even for static and final methods. |
| 62 |
55 #### Useful Tips | 63 #### Useful Tips |
56 | 64 |
57 * Use `@RunWith(LocalRobolectricTestRunner.class)` for all Chromium Robolectric
tests. | 65 * Use `@RunWith(LocalRobolectricTestRunner.class)` for all Chromium Robolectric
tests. |
58 * Use `@Config(manifest = Config.NONE)` for tests. | |
59 Currently, you are unable to pass your app's AndroidManifest to Robolectric. | |
60 * You can specify the Android SDK to run your test with with `@Config(sdk = ??)`
. | 66 * You can specify the Android SDK to run your test with with `@Config(sdk = ??)`
. |
61 | 67 |
62 > Currently, only SDK levels 18, 21, and 25 are supported in Chromium | 68 > Currently, only SDK levels 18, 21, and 25 are supported in Chromium |
63 > but more can be added on request. | 69 > but more can be added on request. |
64 | 70 |
65 #### Example Code | 71 #### Example Code |
66 | 72 |
67 ```java | 73 ```java |
68 package org.chromium.sample.test; | 74 package org.chromium.sample.test; |
69 | 75 |
(...skipping 20 matching lines...) Expand all Loading... |
90 public void exampleTest() { | 96 public void exampleTest() { |
91 String testString = "test"; | 97 String testString = "test"; |
92 | 98 |
93 // Even though these tests runs on the host, Android classes are | 99 // Even though these tests runs on the host, Android classes are |
94 // available to use thanks to Robolectric. | 100 // available to use thanks to Robolectric. |
95 assertTrue(TextUtils.equals(testString, "test")); | 101 assertTrue(TextUtils.equals(testString, "test")); |
96 } | 102 } |
97 } | 103 } |
98 ``` | 104 ``` |
99 | 105 |
| 106 #### Example junit_binary build template. |
| 107 |
| 108 ```python |
| 109 junit_binary("my_robolectric_tests") { |
| 110 |
| 111 java_files = [ |
| 112 "java/src/foo/bar/MyJUnitTest.java" |
| 113 |
| 114 deps = [ |
| 115 "//my/test:dependency", |
| 116 ] |
| 117 |
| 118 # Sets app's package name in Robolectric tests. You need to specify |
| 119 # this variable in order for Robolectric to be able to find your app's |
| 120 # resources. |
| 121 package_name = manifest_package |
| 122 } |
| 123 ``` |
| 124 |
100 #### Example within Chromium | 125 #### Example within Chromium |
101 | 126 |
102 See the [content_junit_tests](https://cs.chromium.org/chromium/src/content/publi
c/android/BUILD.gn) test suite. | 127 See the [content_junit_tests](https://cs.chromium.org/chromium/src/content/publi
c/android/BUILD.gn) test suite. |
103 | 128 |
104 ## Running JUnit tests | 129 ## Running JUnit tests |
105 | 130 |
106 After writing a test, you can run it by: | 131 After writing a test, you can run it by: |
107 | 132 |
108 1. Adding the test file to a `junit_binary` GN target. | 133 1. Adding the test file to a `junit_binary` GN target. |
109 2. Rebuild. | 134 2. Rebuild. |
110 3. GN will generate binary `<out_dir>/bin/run_<suite name>` which | 135 3. GN will generate binary `<out_dir>/bin/run_<suite name>` which |
111 can be used to run your test. | 136 can be used to run your test. |
112 | 137 |
113 For example, the following can be used to run chrome_junit_tests. | 138 For example, the following can be used to run chrome_junit_tests. |
114 | 139 |
115 ```bash | 140 ```bash |
116 # Build the test suite after adding our new test. | 141 # Build the test suite after adding our new test. |
117 ninja -C out/Debug chrome_junit_tests | 142 ninja -C out/Debug chrome_junit_tests |
118 | 143 |
119 # Run the test! | 144 # Run the test! |
120 out/Debug/bin/run_chrome_junit_tests | 145 out/Debug/bin/run_chrome_junit_tests |
121 ``` | 146 ``` |
OLD | NEW |