Skip to content

Repository files navigation

@VisibleForTesting Annotation Processor

Precision access: visible when it matters.

This supports annotation-based processing to reduce access modifiers (e.g., from public to private) or to remove classes, methods, and field signatures as needed.

This is especially useful for unit tests with JUnit.

For example, when you need to provide a test-only parameter for a feature, you can define a method like:

public void setMockupInputForTest(String inputPath) { ... }

To ensure that this method is excluded in the release flavor, declare it as follows:

@VisibleForTesting(scope = VisibleForTesting.Scope.NONE, flavor = "release")
public void setMockupInputForTest(String inputPath) { ... }

1. Supported Scopes

Annotation supports following scopes:

PACKAGE_PRIVATE,
PRIVATE,
PROTECTED,
PUBLIC,
NONE

For scope NONE, this will remove the annotated signature from designated flavor.

2. gradle usage

To ensure this processor to modify the source during the compilation, define as follows:

tasks.register('vftTransform', JavaExec) {
    dependsOn tasks.classes

    def vftOut = layout.buildDirectory.dir("classes-vft/main").get().asFile

    classpath = fileTree(dir: 'libs', include: 'vft-processor-1.0.jar')
    args "$buildDir/classes/java/main", vftOut.absolutePath, buildType

    doLast { sourceSets.main.output.classesDirs.setFrom(files(vftOut)) }
}

jar {
    dependsOn tasks.named('vftTransform')
    ...
}

3. Build

To build this repository, gradle is required.

Run the gradle task jar to build the jar file. or run the command below to build the jar file.

./gradlew jar

In this way, the jar files will be generated at {PROJECT_ROOT}/build/libs/.

vft-processor.jar
vft-processor-ann.jar

Make sure everything is packed properly.

About

Visible For Testing annotation processor

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages