Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the "Best Practices & Limitations" page #301

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

## Best Practices

**DO** test layout inflation in your Robolectric test and ensure that click listeners are set up correctly by testing the `Activity` and `Layout` interaction directly rather than mocking the `LayoutInflater` or providing an abstraction over the view.
**DO** test layout inflation in your Robolectric test and ensure that click listeners are set up correctly by testing the [`Activity`](https://developer.android.com/reference/android/app/Activity) and layout interaction directly, rather than mocking the [`LayoutInflater`](https://developer.android.com/reference/android/view/LayoutInflater) or providing an abstraction over the [`View`](https://developer.android.com/reference/android/view/View).

**DO** use public lifecycle APIs (e.g: via `Robolectric.buildActivity()`) rather than exposing `@VisibleForTesting` methods when testing Android components such as `Activities` and `Services`. Calling those methods directly makes it difficult to refactor the code under test later.
**DO** use public lifecycle APIs (e.g., via [`Robolectric.buildActivity()`](javadoc/latest/org/robolectric/Robolectric.html#buildActivity(java.lang.Class))), rather than exposing `@VisibleForTesting` methods when testing Android components such as `Activity`s and [`Service`](https://developer.android.com/reference/android/app/Service)s. Calling those methods directly makes it difficult to refactor the code under test later.

**DO** limit the number of the threads that are running during each test. Rogue threads often cause test pollution because they are not automatically cleaned up between tests. Oftentimes threads are inadvertently spawned when using third-party libraries (e.g. for networking) or background processing components. One of the main sources of additional threads during tests are `ExecutorService`s that maintain thread pools. If possible, mock dependent components that spawn threads, or use a `DirectExecutor`. If it's necessary to run multiple threads during a test, make sure to explicitly stop all threads and `ExecutorService`s to avoid test pollution.
**DO** limit the number of threads that are running during each test. Rogue threads often cause test pollution because they are not automatically cleaned up between tests. Oftentimes threads are inadvertently spawned when using third-party libraries (e.g., for networking) or background processing components. One of the main sources of additional threads during tests are [`ExecutorService`](https://developer.android.com/reference/kotlin/java/util/concurrent/ExecutorService)s that maintain thread pools. If possible, mock dependent components that spawn threads, or use [`MoreExecutors.directExecutor()`](https://guava.dev/releases/31.1-jre/api/docs/com/google/common/util/concurrent/MoreExecutors.html#directExecutor()). If it's necessary to run multiple threads during a test, make sure to explicitly stop all threads and `ExecutorService`s to avoid test pollution.

**DON'T** mock or spy on Android classes that will be acted on by other Android code (e.g. `Context`, `SharedPreferences`, and many others). Stubbing is very brittle and can lead to breakages on Robolectric or Android Platform upgrades. The small exceptions to this rule are classes with very narrow responsibilities, such as event listeners.
**DON'T** mock or spy on Android classes that will be acted on by other Android code (e.g. [`Context`](https://developer.android.com/reference/android/content/Context), [`SharedPreferences`](https://developer.android.com/reference/android/content/SharedPreferences), and many others). Stubbing is very brittle and can lead to breakages on Robolectric or Android Platform upgrades. The small exceptions to this rule are classes with very narrow responsibilities, such as event listeners.

## Limitations

Expand Down
11 changes: 3 additions & 8 deletions docs/custom-test-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ hide:
There are several situations where you want to customize Robolectric's test runner to perform some operation
before all tests are run, or even before each test method is run. One good example is initializing a dependency
injection framework with a different set of dependencies for your test. Fortunately, Robolectric has a way to
hook into the test lifecycle. If you define an Application class in your AndroidManifest.xml, Robolectric will
automatically try and load a test version of your application class first. For example:

Let's say you've defined a FooApplication in your manifest:

```xml
<application android:name=".FooApplication">
```
hook into the test lifecycle. If you define an [`Application`](https://developer.android.com/reference/android/app/Application)
class in your `AndroidManifest.xml` file, Robolectric will automatically try and load a test version of your
`Application` class first.

## Hilt

Expand Down