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

    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 fields annotated with jakarta.inject.Inject are injected after a test instance is created and unset before a test instance is destroyed. Moreover, the dependent beans injected into fields annotated with jakarta.inject.Inject are correctly destroyed before a test instance is destroyed. Finally, the CDI request context is activated and terminated per each test method.

    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 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:
    InjectMock, TestConfigProperty
    • Constructor Detail

      • 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 -
    • Method Detail

      • 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
      • cast

        static <T> T cast​(Object obj)