Class QuarkusComponentTestExtension

java.lang.Object
io.quarkus.test.component.QuarkusComponentTestExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstancePostProcessor

@Experimental("This feature is experimental and the API may change in the future") public class QuarkusComponentTestExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.ParameterResolver
Makes it easy to test Quarkus components. This extension can be registered declaratively with QuarkusComponentTest or programmatically with a static field of type QuarkusComponentTestExtension, annotated with RegisterExtension and initialized with simplified constructor or using the builder.

Container lifecycle

This extension starts the CDI container and registers a dedicated SmallRyeConfig. If TestInstance.Lifecycle.PER_METHOD is used (default) then the container is started during the before each test phase and stopped during the after each test phase. However, if TestInstance.Lifecycle.PER_CLASS is used then the container is started during the before all test phase and stopped during the after all test phase. The CDI request context is activated and terminated per each test method.

Injection

Test class fields annotated with Inject and InjectMock are injected after a test instance is created and unset before a test instance is destroyed. Dependent beans injected into these fields are correctly destroyed before a test instance is destroyed.

Parameters of a test method for which a matching bean exists are resolved unless annotated with SkipInject. Dependent beans injected into the test method arguments are correctly destroyed after the test method completes.

Auto Mocking Unsatisfied Dependencies

Unlike in regular CDI environments the test does not fail if a component injects an unsatisfied dependency. Instead, a synthetic bean is registered automatically for each combination of required type and qualifiers of an injection point that resolves to an unsatisfied dependency. The bean has the Singleton scope so it's shared across all injection points with the same required type and qualifiers. The injected reference is an unconfigured Mockito mock. You can inject the mock in your test using the InjectMock annotation and leverage the Mockito API to configure the behavior.

Custom Mocks For Unsatisfied Dependencies

Sometimes you need the full control over the bean attributes and maybe even configure the default mock behavior. You can use the mock configurator API via the QuarkusComponentTestExtensionBuilder.mock(Class) method.

See Also:
  • Field Details

  • Constructor Details

    • QuarkusComponentTestExtension

      public QuarkusComponentTestExtension()
    • QuarkusComponentTestExtension

      public QuarkusComponentTestExtension(Class<?>... additionalComponentClasses)
      The initial set of components under test is derived from the test class. The types of all fields annotated with Inject are considered the component types.
      Parameters:
      additionalComponentClasses -
    • QuarkusComponentTestExtension

      QuarkusComponentTestExtension(QuarkusComponentTestConfiguration baseConfiguration)
  • Method Details

    • builder

      public static QuarkusComponentTestExtensionBuilder builder()
    • beforeAll

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

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
      Throws:
      Exception
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      Exception
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • postProcessTestInstance

      public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
      Specified by:
      postProcessTestInstance in interface org.junit.jupiter.api.extension.TestInstancePostProcessor
      Throws:
      Exception
    • supportsParameter

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

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • cast

      static <T> T cast(Object obj)
    • isTestMethod

      static boolean isTestMethod(Executable method)