Class CompilationExtension

java.lang.Object
be.imgn.gaia.processing.testing.CompilationExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstantiationAwareExtension

public final class CompilationExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.ParameterResolver
Gives tests the Elements and Types of a running compilation, to test code that works on javax.lang.model without writing an annotation processor around it.
@ExtendWith(CompilationExtension.class)
class MyTest {
    @Test
    void stringIsACharSequence(Elements elements, Types types) {
        TypeMirror string = elements.getTypeElement("java.lang.String").asType();
        TypeMirror charSequence = elements.getTypeElement("java.lang.CharSequence").asType();
        assertThat(types.isAssignable(string, charSequence)).isTrue();
    }
}

Each test that asks for them gets its own compilation, started by the first parameter that needs it and finished when the test ends. A compilation asked for by a constructor or @BeforeAll parameter is shared by the tests of the class, and so is the one of a class annotated with SharedCompilation. While the test runs, javac waits inside an annotation processor, so the elements and types stay valid; they must not be kept beyond the test. The compilation sees the JDK, and the class path and module path of the running JVM.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates the extension; JUnit does, through @ExtendWith.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    boolean
    supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    getTestInstantiationExtensionContextScope
  • Constructor Details

    • CompilationExtension

      public CompilationExtension()
      Creates the extension; JUnit does, through @ExtendWith.
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver