Class QuarkusComponentTestExtension
- java.lang.Object
-
- io.quarkus.test.component.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.ConfigSourceJUnit 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 alltest phase. The container is stopped and the config is released during theafter alltest phase. The fields annotated withjakarta.inject.Injectare injected after a test instance is created and unset before a test instance is destroyed. Moreover, the dependent beans injected into fields annotated withjakarta.inject.Injectare 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
Singletonscope 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
SmallRyeConfigis registered during thebefore alltest phase. Moreover, it's possible to set the configuration properties via theconfigProperty(String, String)method. If you only need to use the default values for missing config properties, then theuseDefaultConfigProperties()might come in useful.- See Also:
InjectMock,TestConfigProperty
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classQuarkusComponentTestExtension.FieldInjector
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_CONFIG_SOURCE_ORDINALBy default, test config properties take precedence over system properties (400), ENV variables (300) and application.properties (250)
-
Constructor Summary
Constructors Constructor Description QuarkusComponentTestExtension()QuarkusComponentTestExtension(Class<?>... additionalComponentClasses)The initial set of components under test is derived from the test class.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description QuarkusComponentTestExtensionaddAnnotationsTransformer(AnnotationsTransformer transformer)Add an additionalAnnotationsTransformer.voidafterAll(org.junit.jupiter.api.extension.ExtensionContext context)voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context)voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context)voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context)(package private) static <T> Tcast(Object obj)QuarkusComponentTestExtensionconfigProperty(String key, String value)Set a configuration property for the test.StringgetName()intgetOrdinal()Set<String>getPropertyNames()StringgetValue(String propertyName)QuarkusComponentTestExtensionignoreNestedClasses()Ignore the static nested classes declared on the test class.<T> MockBeanConfigurator<T>mock(Class<T> beanClass)Configure a new mock of a bean.voidpostProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context)voidpreDestroyTestInstance(org.junit.jupiter.api.extension.ExtensionContext context)(package private) voidregisterMockBean(MockBeanConfiguratorImpl<?> mock)QuarkusComponentTestExtensionsetConfigSourceOrdinal(int val)Set the ordinal of the config source used for all test config properties.QuarkusComponentTestExtensionuseDefaultConfigProperties()Use the default values for missing config properties.
-
-
-
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 withInjectare 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)
-
configProperty
public QuarkusComponentTestExtension configProperty(String key, String value)
Set a configuration property for the test.- Parameters:
key-value-- Returns:
- self
-
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
nullis 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
-
addAnnotationsTransformer
public QuarkusComponentTestExtension addAnnotationsTransformer(AnnotationsTransformer transformer)
Add an additionalAnnotationsTransformer.- Parameters:
transformer-- Returns:
- self
-
postProcessTestInstance
public void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context) throws Exception
- Specified by:
postProcessTestInstancein interfaceorg.junit.jupiter.api.extension.TestInstancePostProcessor- Throws:
Exception
-
preDestroyTestInstance
public void preDestroyTestInstance(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
preDestroyTestInstancein interfaceorg.junit.jupiter.api.extension.TestInstancePreDestroyCallback- Throws:
Exception
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Throws:
Exception
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback- Throws:
Exception
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
Exception
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Throws:
Exception
-
getPropertyNames
public Set<String> getPropertyNames()
- Specified by:
getPropertyNamesin interfaceorg.eclipse.microprofile.config.spi.ConfigSource
-
getValue
public String getValue(String propertyName)
- Specified by:
getValuein interfaceorg.eclipse.microprofile.config.spi.ConfigSource
-
getName
public String getName()
- Specified by:
getNamein interfaceorg.eclipse.microprofile.config.spi.ConfigSource
-
getOrdinal
public int getOrdinal()
- Specified by:
getOrdinalin interfaceorg.eclipse.microprofile.config.spi.ConfigSource
-
registerMockBean
void registerMockBean(MockBeanConfiguratorImpl<?> mock)
-
cast
static <T> T cast(Object obj)
-
-