blob: fdfdc024d8a9b8d4d15ad33a39015ea4c8e6e7e0 [file] [log] [blame] [view]
mikecased05ab852017-03-15 17:10:171# JUnit Tests
2
3JUnit tests are Java unit tests. These tests run locally on your workstation.
4
5[TOC]
6
7## Writing a JUnit test
8
9When writing JUnit tests, you must decide whether you need to use Android code.
10If you want to use Android code you must write a [Robolectric](http://robolectric.org/) test.
11
12### JUnit tests (without Android)
13
Mohamed Heikal2c5f2fb2022-06-30 18:07:2514Build these types of test using the `robolectric_binary` GN template.
mikecased05ab852017-03-15 17:10:1715
16If you don't need to use any Android code in your tests, you can write plain,
17old JUnit tests. Some more documentation about writing JUnit tests can be
18found [here](https://github.com/junit-team/junit4/wiki/Getting-started).
19
20#### Example Code
21
22```java
23package org.chromium.sample.test;
24
25import static org.junit.Assert.assertTrue;
26
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.BlockJUnit4ClassRunner;
30
31@RunWith(BlockJUnit4ClassRunner.class)
32public class MyJUnitTest {
33
34 @Test
35 public void exampleTest() {
36 boolean shouldWriteMoreJUnitTests = true;
37 assertTrue(shouldWriteMoreJUnitTests);
38 }
39}
40```
41
42#### Example within Chromium
43
44See the [junit_unit_tests](https://cs.chromium.org/chromium/src/testing/android/junit/BUILD.gn) test suite.
45
46### JUnit tests with Robolectric
47
Mohamed Heikal2c5f2fb2022-06-30 18:07:2548Build these types of test using the `robolectric_binary` GN template.
mikecased05ab852017-03-15 17:10:1749
50Robolectric is a unit testing framework that lets you run tests with Android
51code on your workstation. It does this by providing a special version of the
52Android SDK jar that can run in your host JVM. Some more information about
53Robolectric can be found [here](http://robolectric.org/).
54
mikecase5e8650322017-05-16 18:05:1655One on the main benefits of using Robolectric framework are [shadow classes](http://robolectric.org/extending/).
56Robolectric comes with many prebuilt shadow classes and also lets you define
57your own. Whenever an object is instantiated within a Robolectric test,
58Robolectric looks for a corresponding shadow class (marked by
59`@Implements(ClassBeingShadowed.class)`). If found, any time a method is invoked
60on the object, the shadow class's implementation of the method is invoked first.
61This works even for static and final methods.
62
mikecased05ab852017-03-15 17:10:1763#### Useful Tips
64
Ben Joyceeac22e82022-03-23 06:30:0665* Use `@RunWith(BaseRobolectricTestRunner.class)` for all Chromium Robolectric tests.
mikecased05ab852017-03-15 17:10:1766* You can specify the Android SDK to run your test with with `@Config(sdk = ??)`.
67
68> Currently, only SDK levels 18, 21, and 25 are supported in Chromium
69> but more can be added on request.
70
71#### Example Code
72
73```java
74package org.chromium.sample.test;
75
76import static org.junit.Assert.assertTrue;
77
78import android.text.TextUtils;
79
80import org.junit.Test;
81import org.junit.runner.RunWith;
82import org.robolectric.annotation.Config;
83
Ben Joyceeac22e82022-03-23 06:30:0684import org.chromium.base.test.BaseRobolectricTestRunner;
mikecased05ab852017-03-15 17:10:1785
Ben Joyceeac22e82022-03-23 06:30:0686// Be sure to specify to run tests with a RobolectricTestRunner. The
mikecased05ab852017-03-15 17:10:1787// default JUnit test runner won't load the Robolectric Android code properly.
Ben Joyceeac22e82022-03-23 06:30:0688// BaseRobolectricTestRunner will do some common initializations. If this is
Henrique Nakashima485c8acf2023-04-21 20:38:2589// not desired, then RobolectricTestRunner could be used directly.
Ben Joyceeac22e82022-03-23 06:30:0690@RunWith(BaseRobolectricTestRunner.class)
mikecased05ab852017-03-15 17:10:1791// Can specify some Robolectric related configs here.
92// More about configuring Robolectric at http://robolectric.org/configuring/.
93// SDK will default to the latest we support in Chromium.
94@Config(manifest = Config.NONE, sdk = 21)
95public class MyRobolectricJUnitTest {
96
97 @Test
98 public void exampleTest() {
99 String testString = "test";
100
101 // Even though these tests runs on the host, Android classes are
102 // available to use thanks to Robolectric.
103 assertTrue(TextUtils.equals(testString, "test"));
104 }
105}
106```
107
Mohamed Heikal2c5f2fb2022-06-30 18:07:25108#### Example robolectric_binary build template.
mikecase5e8650322017-05-16 18:05:16109
110```python
Mohamed Heikal2c5f2fb2022-06-30 18:07:25111robolectric_binary("my_robolectric_tests") {
mikecase5e8650322017-05-16 18:05:16112
Natalie Chouinardcbdc6dc2019-12-24 00:02:35113 sources = [
mikecase5e8650322017-05-16 18:05:16114 "java/src/foo/bar/MyJUnitTest.java"
Natalie Chouinardcbdc6dc2019-12-24 00:02:35115 ]
mikecase5e8650322017-05-16 18:05:16116
117 deps = [
118 "//my/test:dependency",
119 ]
120
121 # Sets app's package name in Robolectric tests. You need to specify
122 # this variable in order for Robolectric to be able to find your app's
123 # resources.
124 package_name = manifest_package
125}
126```
127
mikecased05ab852017-03-15 17:10:17128#### Example within Chromium
129
130See the [content_junit_tests](https://cs.chromium.org/chromium/src/content/public/android/BUILD.gn) test suite.
131
132## Running JUnit tests
133
134After writing a test, you can run it by:
135
Mohamed Heikal2c5f2fb2022-06-30 18:07:251361. Adding the test file to a `robolectric_binary` GN target.
mikecased05ab852017-03-15 17:10:171372. Rebuild.
1383. GN will generate binary `<out_dir>/bin/run_<suite name>` which
139 can be used to run your test.
140
141For example, the following can be used to run chrome_junit_tests.
142
143```bash
144# Build the test suite after adding our new test.
145ninja -C out/Debug chrome_junit_tests
146
147# Run the test!
148out/Debug/bin/run_chrome_junit_tests
Ben Joyceeac22e82022-03-23 06:30:06149```