# WorkManagerTestInitHelper

Artifact: [androidx.work:work-testing](https://developer.android.com/jetpack/androidx/releases/work) [View Source](https://cs.android.com/search?q=file:androidx/work/testing/WorkManagerTestInitHelper.java+class:androidx.work.testing.WorkManagerTestInitHelper) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

*** ** * ** ***


```
public final class WorkManagerTestInitHelper
```

<br />

*** ** * ** ***

Helps initialize `https://developer.android.com/reference/androidx/work/WorkManager` for testing.

## Summary

| ### Nested types |
|---|
| `public enum https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode` Modes that control which executors are used in tests. |

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#closeWorkDatabase()()` Closes internal `https://developer.android.com/reference/androidx/work/WorkManager`'s database. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/work/testing/TestDriver` | `[getTestDriver](https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver())()` **This method is deprecated.** Call `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver(android.content.Context)` instead. <br /> |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/work/testing/TestDriver` | `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` |
| `static void` | `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` with a `https://developer.android.com/reference/androidx/work/testing/SynchronousExecutor`. |
| `static void` | `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context,androidx.work.Configuration)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Configuration configuration )` Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` with a user-specified `https://developer.android.com/reference/androidx/work/Configuration`, but using `https://developer.android.com/reference/androidx/work/testing/SynchronousExecutor` instead of main thread. |
| `static void` | `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context,androidx.work.testing.WorkManagerTestInitHelper.ExecutorsMode)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode executorsMode )` Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` that can be controlled via `https://developer.android.com/reference/androidx/work/testing/TestDriver`. |
| `static void` | `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context,androidx.work.Configuration,androidx.work.testing.WorkManagerTestInitHelper.ExecutorsMode)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Configuration configuration, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode executorsMode )` Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` that can be controlled via `https://developer.android.com/reference/androidx/work/testing/TestDriver`. |

## Public methods

### closeWorkDatabase

Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/work#2.9.0)

```
public static void closeWorkDatabase()
```

Closes internal `https://developer.android.com/reference/androidx/work/WorkManager`'s database.

It could be helpful to avoid warnings by CloseGuard in testing infra. You need to be make sure that `WorkManager` finished all operations and won't touch database anymore. Meaning that both `https://developer.android.com/reference/androidx/work/Configuration#getTaskExecutor()` and `https://developer.android.com/reference/androidx/work/Configuration#getExecutor()` are idle.

It shouldn't be called from `https://developer.android.com/reference/androidx/work/Configuration#getTaskExecutor()` because this method will block until all internal work is complete after cancellation. To complete this work `https://developer.android.com/reference/androidx/work/Configuration#getTaskExecutor()` could be require and blocking it may lead to deadlocks.

### getTestDriver

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0) Deprecated in [2.1.0](https://developer.android.com/jetpack/androidx/releases/work#2.1.0)

```
public static @Nullable TestDriver [getTestDriver](https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver())()
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Call `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver(android.content.Context)` instead.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/work/testing/TestDriver` | An instance of `https://developer.android.com/reference/androidx/work/testing/TestDriver`. This exposes additional functionality that is useful in the context of testing when using WorkManager. |

### getTestDriver

Added in [2.1.0](https://developer.android.com/jetpack/androidx/releases/work#2.1.0)

```
public static @Nullable TestDriver getTestDriver(@NonNull Context context)
```

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/work/testing/TestDriver` | An instance of `https://developer.android.com/reference/androidx/work/testing/TestDriver`. This exposes additional functionality that is useful in the context of testing when using WorkManager. |

### initializeTestWorkManager

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public static void initializeTestWorkManager(@NonNull Context context)
```

Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` with a `https://developer.android.com/reference/androidx/work/testing/SynchronousExecutor`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The application `https://developer.android.com/reference/android/content/Context.html` |

### initializeTestWorkManager

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/work#1.0.0)

```
public static void initializeTestWorkManager(
    @NonNull Context context,
    @NonNull Configuration configuration
)
```

Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` with a user-specified `https://developer.android.com/reference/androidx/work/Configuration`, but using `https://developer.android.com/reference/androidx/work/testing/SynchronousExecutor` instead of main thread.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The application `https://developer.android.com/reference/android/content/Context.html` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Configuration configuration` | The `https://developer.android.com/reference/androidx/work/Configuration` |

### initializeTestWorkManager

Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/work#2.9.0)

```
public static void initializeTestWorkManager(
    @NonNull Context context,
    @NonNull WorkManagerTestInitHelper.ExecutorsMode executorsMode
)
```

Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` that can be controlled via `https://developer.android.com/reference/androidx/work/testing/TestDriver`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The application `https://developer.android.com/reference/android/content/Context.html` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode executorsMode` | mode controlling executors used by WorkManager in tests. See documentation of modes in `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode` |

### initializeTestWorkManager

Added in [2.9.0](https://developer.android.com/jetpack/androidx/releases/work#2.9.0)

```
public static void initializeTestWorkManager(
    @NonNull Context context,
    @NonNull Configuration configuration,
    @NonNull WorkManagerTestInitHelper.ExecutorsMode executorsMode
)
```

Initializes a test `https://developer.android.com/reference/androidx/work/WorkManager` that can be controlled via `https://developer.android.com/reference/androidx/work/testing/TestDriver`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The application `https://developer.android.com/reference/android/content/Context.html` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/Configuration configuration` | test configuration of WorkManager |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode executorsMode` | mode controlling executors used by WorkManager in tests. See documentation of modes in `https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode` |