# 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.

- `Compiler` lets you choose command-line options, annotation processors, and source files to compile.
- `Compilation` represents the immutable result of compiling source files: diagnostics and generated files.
- `CompilationSubject` lets you make assertions about `Compilation` objects.
- `JavaFileObjectSubject` lets you make assertions about `JavaFileObject` objects.

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);

Class
Description
Compilation
The results of compiling source files.
Compilation.Status
The status of a compilation.
CompilationFailureException
An exception thrown to indicate that compilation has failed for an unknown reason.
CompilationRule
A `JUnit4` `Rule` that executes tests such that a instances of `Elements` and `Types` are available during execution.
CompilationSubject
A `Truth` subject for a `Compilation`.
Compiler
An object that can `Compiler.compile(JavaFileObject...)` Java source files.
CompileTester
The root of the fluent API for testing the result of compilation.
CompileTester.ChainingClause<T>
The clause in the fluent API that allows for chaining test conditions.
CompileTester.CleanCompilationClause
The clause in the fluent API for further tests on successful compilations without warnings.
CompileTester.ColumnClause<T>
The clause in the fluent API that checks that a diagnostic starts at a particular column.
CompileTester.CompilationWithNotesClause<T>
The clause in the fluent API that checks notes in a compilation.
CompileTester.CompilationWithWarningsClause<T>
The clause in the fluent API that checks notes and warnings in a compilation.
CompileTester.FileClause<T>
The clause in the fluent API that checks that a diagnostic is associated with a particular `JavaFileObject`.
CompileTester.GeneratedPredicateClause<T>
The clause in the fluent API that checks that files were generated.
CompileTester.LineClause<T>
The clause in the fluent API that checks that a diagnostic is on a particular line.
CompileTester.SuccessfulCompilationClause
The clause in the fluent API for further tests on successful compilations.
CompileTester.SuccessfulFileClause<T>
The clause in the fluent API that checks that a generated file has the specified contents.
CompileTester.UnsuccessfulCompilationClause
The clause in the fluent API for further tests on unsuccessful compilations.
ForwardingStandardJavaFileManager
Forwards calls to a given `StandardJavaFileManager`.
JavaFileObjects
A utility class for creating `JavaFileObject` instances.
JavaFileObjectSubject
Assertions about `JavaFileObject`s.
JavaSourcesSubject
A Truth `Subject` that evaluates the result of a `javac` compilation.
JavaSourcesSubject.SingleSourceAdapter
JavaSourcesSubjectFactory
A Truth `Subject.Factory` for creating `JavaSourcesSubject` instances.
JavaSourceSubjectFactory
A Truth `Subject.Factory` similar to `JavaSourcesSubjectFactory`, but for working with single source files.
ProcessedCompileTesterFactory
Creates `CompileTester` instances that test compilation with provided `Processor` instances.