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.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.TestInstancePostProcessorMakes it easy to test Quarkus components. This extension can be registered declaratively withQuarkusComponentTestor programmatically with a static field of typeQuarkusComponentTestExtension, annotated withRegisterExtensionand initialized withsimplified constructoror using thebuilder.This extension starts the CDI container and registers a dedicated SmallRyeConfig. If
TestInstance.Lifecycle.PER_METHODis used (default) then the container is started during thebefore eachtest phase and stopped during theafter eachtest phase. However, ifTestInstance.Lifecycle.PER_CLASSis used then the container is started during thebefore alltest phase and stopped 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
QuarkusComponentTestExtensionBuilder.mock(Class)method.- See Also:
InjectMock,TestConfigProperty
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classQuarkusComponentTestExtension.FieldInjector
-
Constructor Summary
Constructors Constructor Description QuarkusComponentTestExtension()QuarkusComponentTestExtension(QuarkusComponentTestConfiguration baseConfiguration)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 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)static QuarkusComponentTestExtensionBuilderbuilder()(package private) static <T> Tcast(Object obj)voidpostProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context)
-
-
-
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-
-
QuarkusComponentTestExtension
QuarkusComponentTestExtension(QuarkusComponentTestConfiguration baseConfiguration)
-
-
Method Detail
-
builder
public static QuarkusComponentTestExtensionBuilder builder()
-
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
-
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
-
cast
static <T> T cast(Object obj)
-
-