Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

VisibleForTesting

class VisibleForTesting
androidx.annotation.VisibleForTesting

Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable.

You can optionally specify what the visibility should have been if not for testing; this allows tools to catch unintended access from within production code.

Example:

<code>
   @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
   public String printDiagnostics() { ... }
  </code>
If not specified, the intended visibility is assumed to be private.

Summary

Constants

static Int

The annotated element should never be called from production code, only from tests.

static Int

The annotated element would have "package private" visibility

static Int

The annotated element would have "private" visibility

static Int

The annotated element would have "protected" visibility

Public constructors

<init>(otherwise: Int)

Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable.

Properties

Int

The visibility the annotated element would have if it did not need to be made visible for testing.

Constants

NONE

static val NONE: Int

The annotated element should never be called from production code, only from tests.

This is equivalent to @RestrictTo.Scope.TESTS.

Value: 5

PACKAGE_PRIVATE

static val PACKAGE_PRIVATE: Int

The annotated element would have "package private" visibility

Value: 3

PRIVATE

static val PRIVATE: Int

The annotated element would have "private" visibility

Value: 2

PROTECTED

static val PROTECTED: Int

The annotated element would have "protected" visibility

Value: 4

Public constructors

<init>

VisibleForTesting(otherwise: Int)

Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable.

You can optionally specify what the visibility should have been if not for testing; this allows tools to catch unintended access from within production code.

Example:

<code>
   @VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
   public String printDiagnostics() { ... }
  </code>
If not specified, the intended visibility is assumed to be private.

Properties

otherwise

val otherwise: Int

The visibility the annotated element would have if it did not need to be made visible for testing.