All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.testing.compile.package-info Maven / Gradle / Ivy

There is a newer version: 0.21.0
Show newest version
/*
 * Copyright (C) 2013 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * This package contains classes that let you compile Java source code in tests and make assertions
 * about the results. This lets you easily test {@linkplain javax.annotation.processing.Processor
 * annotation processors} without forking {@code javac} or creating separate integration test
 * projects.
 *
 * 
    *
  • {@link Compiler} lets you choose command-line options, annotation processors, and source * files to compile. *
  • {@link Compilation} represents the immutable result of compiling source files: diagnostics * and generated files. *
  • {@link CompilationSubject} lets you make assertions about {@link Compilation} objects. *
  • {@link JavaFileObjectSubject} lets you make assertions about {@link * javax.tools.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);
 * 
*/ @CheckReturnValue package com.google.testing.compile; import javax.annotation.CheckReturnValue;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy