WorkManagerTestInitHelper


class WorkManagerTestInitHelper


Helps initialize androidx.work.WorkManager for testing.

Summary

Nested types

Modes that control which executors are used in tests.

Public functions

java-static Unit

Closes internal androidx.work.WorkManager's database.

java-static TestDriver?

This function is deprecated.

Call getTestDriver instead.

java-static TestDriver?
java-static Unit

Initializes a test androidx.work.WorkManager with a SynchronousExecutor.

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

Initializes a test androidx.work.WorkManager with a user-specified androidx.work.Configuration, but using SynchronousExecutor instead of main thread.

java-static Unit

Initializes a test androidx.work.WorkManager that can be controlled via .

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

Initializes a test androidx.work.WorkManager that can be controlled via .

Public functions

closeWorkDatabase

Added in 2.9.0
java-static fun closeWorkDatabase(): Unit

Closes internal 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 and getExecutor are idle.

It shouldn't be called from getTaskExecutor because this method will block until all internal work is complete after cancellation. To complete this work getTaskExecutor could be require and blocking it may lead to deadlocks.

getTestDriver

Added in 1.0.0
Deprecated in 2.1.0
java-static fun getTestDriver(): TestDriver?
Returns
TestDriver?

An instance of TestDriver. This exposes additional functionality that is useful in the context of testing when using WorkManager.

getTestDriver

Added in 2.1.0
java-static fun getTestDriver(context: Context): TestDriver?
Returns
TestDriver?

An instance of TestDriver. This exposes additional functionality that is useful in the context of testing when using WorkManager.

initializeTestWorkManager

Added in 1.0.0
java-static fun initializeTestWorkManager(context: Context): Unit

Initializes a test androidx.work.WorkManager with a SynchronousExecutor.

Parameters
context: Context

The application Context

initializeTestWorkManager

Added in 1.0.0
java-static fun initializeTestWorkManager(context: Context, configuration: Configuration): Unit

Initializes a test androidx.work.WorkManager with a user-specified androidx.work.Configuration, but using SynchronousExecutor instead of main thread.

Parameters
context: Context

The application Context

configuration: Configuration

The androidx.work.Configuration

initializeTestWorkManager

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

Initializes a test androidx.work.WorkManager that can be controlled via .

Parameters
context: Context

The application Context

executorsMode: WorkManagerTestInitHelper.ExecutorsMode

mode controlling executors used by WorkManager in tests. See documentation of modes in ExecutorsMode

initializeTestWorkManager

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

Initializes a test androidx.work.WorkManager that can be controlled via .

Parameters
context: Context

The application Context

configuration: Configuration

test configuration of WorkManager

executorsMode: WorkManagerTestInitHelper.ExecutorsMode

mode controlling executors used by WorkManager in tests. See documentation of modes in ExecutorsMode