WorkManagerTestInitHelper


public final class WorkManagerTestInitHelper


Helps initialize androidx.work.WorkManager for testing.

Summary

Nested types

Modes that control which executors are used in tests.

Public methods

static void

Closes internal androidx.work.WorkManager's database.

static @Nullable TestDriver

This method is deprecated.

Call getTestDriver instead.

static @Nullable TestDriver
static void

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

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

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

static void

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

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

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

Public methods

closeWorkDatabase

Added in 2.9.0
public static void closeWorkDatabase()

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
public static @Nullable TestDriver getTestDriver()
Returns
@Nullable 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
public static @Nullable TestDriver getTestDriver(@NonNull Context context)
Returns
@Nullable 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
public static void initializeTestWorkManager(@NonNull Context context)

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

Parameters
@NonNull Context context

The application Context

initializeTestWorkManager

Added in 1.0.0
public static void initializeTestWorkManager(
    @NonNull Context context,
    @NonNull Configuration configuration
)

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

Parameters
@NonNull Context context

The application Context

@NonNull Configuration configuration

The androidx.work.Configuration

initializeTestWorkManager

Added in 2.9.0
public static void initializeTestWorkManager(
    @NonNull Context context,
    @NonNull WorkManagerTestInitHelper.ExecutorsMode executorsMode
)

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

Parameters
@NonNull Context context

The application Context

@NonNull WorkManagerTestInitHelper.ExecutorsMode executorsMode

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

initializeTestWorkManager

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

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

Parameters
@NonNull Context context

The application Context

@NonNull Configuration configuration

test configuration of WorkManager

@NonNull WorkManagerTestInitHelper.ExecutorsMode executorsMode

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