VisibleForTesting  |  API reference  |  Android Developers (original) (raw)

Stay organized with collections Save and categorize content based on your preferences.


@MustBeDocumented
@Retention(value = AnnotationRetention.BINARY)
public 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:

@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
public String printDiagnostics() { ... }

If not specified, the intended visibility is assumed to be private.

Summary

Constants
static final int NONE = 5 The annotated element should never be called from production code, only from tests.
static final int PACKAGE_PRIVATE = 3 The annotated element would have package private visibility.
static final int PRIVATE = 2 The annotated element would have private visibility.
static final int PROTECTED = 4 The annotated element would have protected visibility.
Public methods
final int getOtherwise() The visibility the annotated element would have if it did not need to be made visible for testing.

Constants

NONE

public static final int NONE = 5

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

This is equivalent to [RestrictTo.Scope.TESTS](/reference/androidx/annotation/RestrictTo.Scope#TESTS).

PACKAGE_PRIVATE

public static final int PACKAGE_PRIVATE = 3

The annotated element would have package private visibility.

PRIVATE

public static final int PRIVATE = 2

The annotated element would have private visibility.

PROTECTED

public static final int PROTECTED = 4

The annotated element would have protected visibility.

Public constructors

Public methods

getOtherwise

public final int getOtherwise()

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

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2025-02-10 UTC.