Commit 5e865032 authored by mikecase's avatar mikecase Committed by Commit bot

Updated Robolectric documentation.

Now that change to expose resources to Robolectric tests has
landed, I wanted to update the documentation to point out to
people they need to specify "package_name" GN variable to get
it to work (since that is definitely not obvious).

You can also (theoretically) now specify the AndroidManifest.xml
file to Robolectric (Robolectric will grab the package name from
there). However, no tests currently do this.

Review-Url: https://codereview.chromium.org/2882243003
Cr-Commit-Position: refs/heads/master@{#472156}
parent ae01bc6a
......@@ -52,11 +52,17 @@ code on your workstation. It does this by providing a special version of the
Android SDK jar that can run in your host JVM. Some more information about
Robolectric can be found [here](http://robolectric.org/).
One on the main benefits of using Robolectric framework are [shadow classes](http://robolectric.org/extending/).
Robolectric comes with many prebuilt shadow classes and also lets you define
your own. Whenever an object is instantiated within a Robolectric test,
Robolectric looks for a corresponding shadow class (marked by
`@Implements(ClassBeingShadowed.class)`). If found, any time a method is invoked
on the object, the shadow class's implementation of the method is invoked first.
This works even for static and final methods.
#### Useful Tips
* Use `@RunWith(LocalRobolectricTestRunner.class)` for all Chromium Robolectric tests.
* Use `@Config(manifest = Config.NONE)` for tests.
Currently, you are unable to pass your app's AndroidManifest to Robolectric.
* You can specify the Android SDK to run your test with with `@Config(sdk = ??)`.
> Currently, only SDK levels 18, 21, and 25 are supported in Chromium
......@@ -97,6 +103,25 @@ public class MyRobolectricJUnitTest {
}
```
#### Example junit_binary build template.
```python
junit_binary("my_robolectric_tests") {
java_files = [
"java/src/foo/bar/MyJUnitTest.java"
deps = [
"//my/test:dependency",
]
# Sets app's package name in Robolectric tests. You need to specify
# this variable in order for Robolectric to be able to find your app's
# resources.
package_name = manifest_package
}
```
#### Example within Chromium
See the [content_junit_tests](https://cs.chromium.org/chromium/src/content/public/android/BUILD.gn) test suite.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment