Class QuarkusComponentTestExtension

  • All Implemented Interfaces:
    org.eclipse.microprofile.config.spi.ConfigSource, 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, org.junit.jupiter.api.extension.TestInstancePreDestroyCallback

    @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.TestInstancePreDestroyCallback, org.eclipse.microprofile.config.spi.ConfigSource
    JUnit extension that makes it easy to test Quarkus components, aka the CDI beans.

    Lifecycle

    The CDI container is started and a dedicated SmallRyeConfig is registered during the before all test phase. The container is stopped and the config is released 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 mock(Class) method.

    Configuration

    A dedicated SmallRyeConfig is registered during the before all test phase. Moreover, it's possible to set the configuration properties via the configProperty(String, String) method. If you only need to use the default values for missing config properties, then the useDefaultConfigProperties() might come in useful.

    See Also:
    InjectMock, TestConfigProperty
    • Field Detail

      • DEFAULT_CONFIG_SOURCE_ORDINAL

        public static final int DEFAULT_CONFIG_SOURCE_ORDINAL
        By default, test config properties take precedence over system properties (400), ENV variables (300) and application.properties (250)
        See Also:
        Constant Field Values
    • 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

      • mock

        public <T> MockBeanConfigurator<T> mock​(Class<T> beanClass)
        Configure a new mock of a bean.

        Note that a mock is created automatically for all unsatisfied dependencies in the test. This API provides full control over the bean attributes. The default values are derived from the bean class.

        Parameters:
        beanClass -
        Returns:
        a new mock bean configurator
        See Also:
        MockBeanConfigurator.create(Function)
      • useDefaultConfigProperties

        public QuarkusComponentTestExtension useDefaultConfigProperties()
        Use the default values for missing config properties. By default, a missing config property results in a test failure.

        For primitives the default values as defined in the JLS are used. For any other type null is injected.

        Returns:
        self
      • ignoreNestedClasses

        public QuarkusComponentTestExtension ignoreNestedClasses()
        Ignore the static nested classes declared on the test class.

        By default, all static nested classes declared on the test class are added to the set of additional components under test.

        Returns:
        self
      • setConfigSourceOrdinal

        public QuarkusComponentTestExtension setConfigSourceOrdinal​(int val)
        Set the ordinal of the config source used for all test config properties. By default, 500 is used.
        Parameters:
        val -
        Returns:
        self
      • 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
      • preDestroyTestInstance

        public void preDestroyTestInstance​(org.junit.jupiter.api.extension.ExtensionContext context)
                                    throws Exception
        Specified by:
        preDestroyTestInstance in interface org.junit.jupiter.api.extension.TestInstancePreDestroyCallback
        Throws:
        Exception
      • 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
      • getPropertyNames

        public Set<String> getPropertyNames()
        Specified by:
        getPropertyNames in interface org.eclipse.microprofile.config.spi.ConfigSource
      • getValue

        public String getValue​(String propertyName)
        Specified by:
        getValue in interface org.eclipse.microprofile.config.spi.ConfigSource
      • getName

        public String getName()
        Specified by:
        getName in interface org.eclipse.microprofile.config.spi.ConfigSource
      • getOrdinal

        public int getOrdinal()
        Specified by:
        getOrdinal in interface org.eclipse.microprofile.config.spi.ConfigSource
      • cast

        static <T> T cast​(Object obj)