# WorkManagerTestInitHelper

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper "View this page in Java")

<br />

```
class WorkManagerTestInitHelper
```

<br />

*** ** * ** ***

Helps initialize[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)for testing.

## Summary

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

|                                              ### Public functions                                              |
|----------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                    | [closeWorkDatabase](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#closeWorkDatabase())`()` Closes internal[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)'s database.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `java-static `[TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver)`?` | ~~[getTestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver())~~`()` **This function is deprecated.** Call[getTestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver(android.content.Context))instead. <br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `java-static `[TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver)`?` | [getTestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver(android.content.Context))`(context: `[Context](https://developer.android.com/reference/android/content/Context.html)`)`                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                    | [initializeTestWorkManager](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context))`(context: `[Context](https://developer.android.com/reference/android/content/Context.html)`)` Initializes a test[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)with a[SynchronousExecutor](https://developer.android.com/reference/kotlin/androidx/work/testing/SynchronousExecutor).                                                                                                                                                                                                                                                                                                                                                                                                |
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                    | [initializeTestWorkManager](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context,androidx.work.Configuration))`(context: `[Context](https://developer.android.com/reference/android/content/Context.html)`, configuration: `[Configuration](https://developer.android.com/reference/kotlin/androidx/work/Configuration)`)` Initializes a test[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)with a user-specified[androidx.work.Configuration](https://developer.android.com/reference/kotlin/androidx/work/Configuration), but using[SynchronousExecutor](https://developer.android.com/reference/kotlin/androidx/work/testing/SynchronousExecutor)instead of main thread.                                                                                             |
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                    | [initializeTestWorkManager](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context,androidx.work.testing.WorkManagerTestInitHelper.ExecutorsMode))`(` ` context: `[Context](https://developer.android.com/reference/android/content/Context.html)`,` ` executorsMode: `[WorkManagerTestInitHelper.ExecutorsMode](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode) `)` Initializes a test[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)that can be controlled via[TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver).                                                                                                                                              |
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                    | [initializeTestWorkManager](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#initializeTestWorkManager(android.content.Context,androidx.work.Configuration,androidx.work.testing.WorkManagerTestInitHelper.ExecutorsMode))`(` ` context: `[Context](https://developer.android.com/reference/android/content/Context.html)`,` ` configuration: `[Configuration](https://developer.android.com/reference/kotlin/androidx/work/Configuration)`,` ` executorsMode: `[WorkManagerTestInitHelper.ExecutorsMode](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode) `)` Initializes a test[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)that can be controlled via[TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver). |

## Public functions

### closeWorkDatabase

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

```
java-static fun closeWorkDatabase(): Unit
```

Closes internal[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/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[getTaskExecutor](https://developer.android.com/reference/kotlin/androidx/work/Configuration#getTaskExecutor())and[getExecutor](https://developer.android.com/reference/kotlin/androidx/work/Configuration#getExecutor())are idle.

It shouldn't be called from[getTaskExecutor](https://developer.android.com/reference/kotlin/androidx/work/Configuration#getTaskExecutor())because this method will block until all internal work is complete after cancellation. To complete this work[getTaskExecutor](https://developer.android.com/reference/kotlin/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)  

```
java-static fun [getTestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver())(): TestDriver?
```
| **This function is deprecated.**   
|
Call[getTestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper#getTestDriver(android.content.Context))instead.  

|                                             Returns                                              |
|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver)`?` | An instance of[TestDriver](https://developer.android.com/reference/kotlin/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)  

```
java-static fun getTestDriver(context: Context): TestDriver?
```  

|                                             Returns                                              |
|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver)`?` | An instance of[TestDriver](https://developer.android.com/reference/kotlin/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)  

```
java-static fun initializeTestWorkManager(context: Context): Unit
```

Initializes a test[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)with a[SynchronousExecutor](https://developer.android.com/reference/kotlin/androidx/work/testing/SynchronousExecutor).  

|                                         Parameters                                         |
|--------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| `context: `[Context](https://developer.android.com/reference/android/content/Context.html) | The application[Context](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)  

```
java-static fun initializeTestWorkManager(context: Context, configuration: Configuration): Unit
```

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

|                                                  Parameters                                                  |
|--------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|
| `context: `[Context](https://developer.android.com/reference/android/content/Context.html)                   | The application[Context](https://developer.android.com/reference/android/content/Context.html)               |
| `configuration: `[Configuration](https://developer.android.com/reference/kotlin/androidx/work/Configuration) | The[androidx.work.Configuration](https://developer.android.com/reference/kotlin/androidx/work/Configuration) |

### initializeTestWorkManager

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

```
java-static fun initializeTestWorkManager(
    context: Context,
    executorsMode: WorkManagerTestInitHelper.ExecutorsMode
): Unit
```

Initializes a test[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)that can be controlled via[TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver).  

|                                                                                Parameters                                                                                |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context: `[Context](https://developer.android.com/reference/android/content/Context.html)                                                                               | The application[Context](https://developer.android.com/reference/android/content/Context.html)                                                                                                                      |
| `executorsMode: `[WorkManagerTestInitHelper.ExecutorsMode](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode) | mode controlling executors used by WorkManager in tests. See documentation of modes in[ExecutorsMode](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode) |

### initializeTestWorkManager

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

```
java-static fun initializeTestWorkManager(
    context: Context,
    configuration: Configuration,
    executorsMode: WorkManagerTestInitHelper.ExecutorsMode
): Unit
```

Initializes a test[androidx.work.WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)that can be controlled via[TestDriver](https://developer.android.com/reference/kotlin/androidx/work/testing/TestDriver).  

|                                                                                Parameters                                                                                |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `context: `[Context](https://developer.android.com/reference/android/content/Context.html)                                                                               | The application[Context](https://developer.android.com/reference/android/content/Context.html)                                                                                                                      |
| `configuration: `[Configuration](https://developer.android.com/reference/kotlin/androidx/work/Configuration)                                                             | test configuration of WorkManager                                                                                                                                                                                   |
| `executorsMode: `[WorkManagerTestInitHelper.ExecutorsMode](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode) | mode controlling executors used by WorkManager in tests. See documentation of modes in[ExecutorsMode](https://developer.android.com/reference/kotlin/androidx/work/testing/WorkManagerTestInitHelper.ExecutorsMode) |