Package com.google.testing.compile
@CheckReturnValue
@NullMarked
package com.google.testing.compile
This package contains classes that let you compile Java source code in tests and make assertions
about the results. This lets you easily test annotation processors without forking
javac or creating separate integration test
projects.
Compilerlets you choose command-line options, annotation processors, and source files to compile.Compilationrepresents the immutable result of compiling source files: diagnostics and generated files.CompilationSubjectlets you make assertions aboutCompilationobjects.JavaFileObjectSubjectlets you make assertions aboutJavaFileObjectobjects.
A simple example that tests that compiling a source file succeeded is:
Compilation compilation =
javac().compile(JavaFileObjects.forSourceString("HelloWorld", "final class HelloWorld {}"));
assertThat(compilation).succeeded();
A similar example that tests that compiling a source file with an annotation processor succeeded without errors or warnings (including compiling any source files generated by the annotation processor) is:
Compilation compilation =
javac()
.withProcessors(new MyAnnotationProcessor())
.compile(JavaFileObjects.forSourceString("HelloWorld", "final class HelloWorld {}"));
assertThat(compilation).succeededWithoutWarnings();
You can make assertions about the files generated during the compilation as well. For example, the following snippet tests that compiling a source file with an annotation processor generates a source file equivalent to a golden file:
Compilation compilation =
javac()
.withProcessors(new MyAnnotationProcessor())
.compile(JavaFileObjects.forResource("HelloWorld.java"));
assertThat(compilation).succeeded();
assertThat(compilation)
.generatedSourceFile("GeneratedHelloWorld")
.hasSourceEquivalentTo(JavaFileObjects.forResource("GeneratedHelloWorld.java"));
You can also test that errors or other diagnostics were reported. The following tests that compiling a source file with an annotation processor reported an error:
JavaFileObject helloWorld = JavaFileObjects.forResource("HelloWorld.java");
Compilation compilation =
javac()
.withProcessors(new NoHelloWorld())
.compile(helloWorld);
assertThat(compilation).failed();
assertThat(compilation)
.hadErrorContaining("No types named HelloWorld!")
.inFile(helloWorld)
.onLine(23)
.atColumn(5);
-
ClassDescriptionThe results of compiling source files.The status of a compilation.An exception thrown to indicate that compilation has failed for an unknown reason.A
Truthsubject for aCompilation.An object that canCompiler.compile(JavaFileObject...)Java source files.The root of the fluent API for testing the result of compilation.The clause in the fluent API that allows for chaining test conditions.The clause in the fluent API for further tests on successful compilations without warnings.The clause in the fluent API that checks that a diagnostic starts at a particular column.The clause in the fluent API that checks notes in a compilation.The clause in the fluent API that checks notes and warnings in a compilation.The clause in the fluent API that checks that a diagnostic is associated with a particularJavaFileObject.The clause in the fluent API that checks that files were generated.The clause in the fluent API that checks that a diagnostic is on a particular line.The clause in the fluent API for further tests on successful compilations.The clause in the fluent API that checks that a generated file has the specified contents.The clause in the fluent API for further tests on unsuccessful compilations.Forwards calls to a givenStandardJavaFileManager.A utility class for creatingJavaFileObjectinstances.Assertions aboutJavaFileObjects.A TruthSubject.Factorysimilar toJavaSourcesSubjectFactory, but for working with single source files.CreatesCompileTesterinstances that test compilation with providedProcessorinstances.