Class AbstractMicronautExtension<C>
java.lang.Object
io.micronaut.test.extensions.AbstractMicronautExtension<C>
- Type Parameters:
C- The extension context
- All Implemented Interfaces:
TestExecutionListener, TestMethodInterceptor<Object>
- Direct Known Subclasses:
MicronautJunit5Extension, MicronautSpockExtension
public abstract class AbstractMicronautExtension<C>
extends Object
implements TestExecutionListener, TestMethodInterceptor<Object>
Abstract base class for both JUnit 5 and Spock.
- Since:
- 1.0
- Author:
- graemerocher
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected io.micronaut.context.ApplicationContextstatic final StringSystem property limiting how many test application contexts may be active at the same time.static final Stringprotected io.micronaut.runtime.EmbeddedApplication<?> static final Stringstatic final Stringprotected io.micronaut.runtime.context.scope.refresh.RefreshScopeprotected io.micronaut.inject.BeanDefinition<?> static final StringThe name of the property source that contains test properties.static final Stringstatic final Stringstatic final Stringstatic final Stringprotected MicronautTestValueprotected Class<?> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidafterClass(C context) Executed after the class is complete.voidafterCleanupTest(TestContext testContext) Executed after the setup method of a test method.voidExecuted after each test completes.voidafterSetupTest(TestContext testContext) Executed after the setup method of a test method.voidafterTestClass(TestContext testContext) Executed after all of the tests of a class have bean executed.voidafterTestExecution(TestContext testContext) Executed after a single test iteration has been executed .voidafterTestMethod(TestContext testContext) Executed after a test method has been executed (a test method may contain multiple iterations).protected abstract voidalignMocks(C context, Object instance) protected voidapplicationContextRebuilt(C context) Called afterrebuildContext = truereplaced the application context before a test, once the new context, and its embedded application if any, have started.protected voidbeforeClass(C context, Class<?> testClass, @Nullable MicronautTestValue testAnnotationValue) Executed before tests within a class are run.voidbeforeCleanupTest(TestContext testContext) Executed before the setup method of a test method.protected voidbeforeEach(C context, @Nullable Object testInstance, @Nullable AnnotatedElement method, List<io.micronaut.context.annotation.Property> propertyAnnotations) To be called by the different implementations before each test method.protected voidbeforeInjectTestInstance(C context, Object testInstance) Allows implementations to inject enclosing or related test instances before the current test instance is injected and mock fields are aligned.voidbeforeSetupTest(TestContext testContext) Executed before the setup method of a test method.voidbeforeTestClass(TestContext testContext) Executed before all of the tests of a class are executed.voidbeforeTestExecution(TestContext testContext) Executed before a single test iteration is executed.voidbeforeTestMethod(TestContext testContext) Executed before a test method is executed (a test method may contain multiple iterations).protected booleanprotected voidinjectTestInstance(Object testInstance) Injects an instance of the test class, or of a class nested in it, from the running context.interceptAfterEach(TestMethodInvocationContext<Object> methodInvocationContext) Intercepts after each test invocation.interceptBeforeEach(TestMethodInvocationContext<Object> methodInvocationContext) Intercepts before each test invocation.protected ObjectinterceptClassLifecycleMethod(TestMethodInvocationContext<Object> methodInvocationContext) Applies the Micronaut around advice declared on a class level lifecycle method of the test instance, such as a non-static JUnit@BeforeAllmethod.interceptTest(TestMethodInvocationContext<Object> methodInvocationContext) Intercepts each test invocation.protected booleanWhether the tests run inside a native image, where generated bean definition classes cannot be looked up by name.protected booleanisTestSuiteBeanPresent(Class<?> requiredTestClass) protected voidpostProcessBuilder(io.micronaut.context.ApplicationContextBuilder builder) Allows subclasses to customize the builder right before context initialization.protected abstract voidresolveTestProperties(C context, MicronautTestValue testAnnotationValue, Map<String, Object> testProperties) Resolves any test properties.protected booleanWhether this context runs under theCONTEXT_PARALLELISM_PROPERTYpermit of another context that stays active around it, and which it would otherwise wait for forever.protected voidStarts the application context.voidtestAborted(TestContext testContext) Executed after a test was aborted, for example by a failed assumption.voidtestDisabled(TestContext testContext, String reason) Executed when a test was never run because it was disabled.voidtestFailed(TestContext testContext) Executed after a test failed.voidtestSuccessful(TestContext testContext) Executed after a test completed successfully.
-
Field Details
-
NATIVE_IMAGE_CODE_PROPERTY
- See Also:
-
CONTEXT_PARALLELISM_PROPERTY
System property limiting how many test application contexts may be active at the same time.- See Also:
-
TEST_ROLLBACK
- See Also:
-
TEST_TRANSACTIONAL
- See Also:
-
TEST_TRANSACTION_MODE
- See Also:
-
TEST_TRANSACTIONAL_DEFAULT
- See Also:
-
DISABLED_MESSAGE
- See Also:
-
MISCONFIGURED_MESSAGE
- See Also:
-
TEST_PROPERTY_SOURCE
The name of the property source that contains test properties.- See Also:
-
applicationContext
protected io.micronaut.context.ApplicationContext applicationContext -
embeddedApplication
protected io.micronaut.runtime.EmbeddedApplication<?> embeddedApplication -
refreshScope
protected io.micronaut.runtime.context.scope.refresh.RefreshScope refreshScope -
specDefinition
protected io.micronaut.inject.BeanDefinition<?> specDefinition -
testProperties
-
oldValues
-
testAnnotationValue
-
testClass
-
-
Constructor Details
-
AbstractMicronautExtension
public AbstractMicronautExtension()
-
-
Method Details
-
hasInterceptors
protected boolean hasInterceptors()- Returns:
- True if there are interceptors
-
interceptBeforeEach
public Object interceptBeforeEach(TestMethodInvocationContext<Object> methodInvocationContext) throws Throwable Description copied from interface:TestMethodInterceptorIntercepts before each test invocation.- Specified by:
interceptBeforeEachin interfaceTestMethodInterceptor<C>- Parameters:
methodInvocationContext- the method invocation context- Returns:
- intercepted return value
- Throws:
Throwable
-
interceptClassLifecycleMethod
protected Object interceptClassLifecycleMethod(TestMethodInvocationContext<Object> methodInvocationContext) throws Throwable Applies the Micronaut around advice declared on a class level lifecycle method of the test instance, such as a non-static JUnit@BeforeAllmethod.TestMethodInterceptors are not applied, because they have no class level callbacks.- Parameters:
methodInvocationContext- The invocation context- Returns:
- The result of the method
- Throws:
Throwable- if the method fails
-
interceptAfterEach
public Object interceptAfterEach(TestMethodInvocationContext<Object> methodInvocationContext) throws Throwable Description copied from interface:TestMethodInterceptorIntercepts after each test invocation.- Specified by:
interceptAfterEachin interfaceTestMethodInterceptor<C>- Parameters:
methodInvocationContext- the method invocation context- Returns:
- intercepted return value
- Throws:
Throwable
-
interceptTest
public Object interceptTest(TestMethodInvocationContext<Object> methodInvocationContext) throws Throwable Description copied from interface:TestMethodInterceptorIntercepts each test invocation.- Specified by:
interceptTestin interfaceTestMethodInterceptor<C>- Parameters:
methodInvocationContext- the method invocation context- Returns:
- intercepted return value
- Throws:
Throwable
-
beforeTestExecution
Description copied from interface:TestExecutionListenerExecuted before a single test iteration is executed.- Specified by:
beforeTestExecutionin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
beforeCleanupTest
Description copied from interface:TestExecutionListenerExecuted before the setup method of a test method.- Specified by:
beforeCleanupTestin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
afterCleanupTest
Description copied from interface:TestExecutionListenerExecuted after the setup method of a test method.- Specified by:
afterCleanupTestin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
afterTestExecution
Description copied from interface:TestExecutionListenerExecuted after a single test iteration has been executed .- Specified by:
afterTestExecutionin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
beforeTestClass
Description copied from interface:TestExecutionListenerExecuted before all of the tests of a class are executed.- Specified by:
beforeTestClassin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
testDisabled
Description copied from interface:TestExecutionListenerExecuted when a test was never run because it was disabled.None of the other callbacks fire for a disabled test, so this is the only notification a listener receives for it.
- Specified by:
testDisabledin interfaceTestExecutionListener- Parameters:
testContext- the test contextreason- the reason the test was disabled, if the test framework supplied one- Throws:
Exception- allows any exception to propagate
-
testSuccessful
Description copied from interface:TestExecutionListenerExecuted after a test completed successfully.- Specified by:
testSuccessfulin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
testAborted
Description copied from interface:TestExecutionListenerExecuted after a test was aborted, for example by a failed assumption. An aborted test is not a failure;TestContext.getTestException()holds the throwable that aborted it.- Specified by:
testAbortedin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
testFailed
Description copied from interface:TestExecutionListenerExecuted after a test failed.TestContext.getTestException()holds the failure.- Specified by:
testFailedin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
afterTestClass
Description copied from interface:TestExecutionListenerExecuted after all of the tests of a class have bean executed.- Specified by:
afterTestClassin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
beforeSetupTest
Description copied from interface:TestExecutionListenerExecuted before the setup method of a test method.- Specified by:
beforeSetupTestin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
afterSetupTest
Description copied from interface:TestExecutionListenerExecuted after the setup method of a test method.- Specified by:
afterSetupTestin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
beforeTestMethod
Description copied from interface:TestExecutionListenerExecuted before a test method is executed (a test method may contain multiple iterations).- Specified by:
beforeTestMethodin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
afterTestMethod
Description copied from interface:TestExecutionListenerExecuted after a test method has been executed (a test method may contain multiple iterations).- Specified by:
afterTestMethodin interfaceTestExecutionListener- Parameters:
testContext- the test context- Throws:
Exception- allows any exception to propagate
-
beforeClass
protected void beforeClass(C context, Class<?> testClass, @Nullable MicronautTestValue testAnnotationValue) Executed before tests within a class are run.- Parameters:
context- The test contexttestClass- The test classtestAnnotationValue- The test annotation values
-
postProcessBuilder
protected void postProcessBuilder(io.micronaut.context.ApplicationContextBuilder builder) Allows subclasses to customize the builder right before context initialization.- Parameters:
builder- the application context builder
-
resolveTestProperties
protected abstract void resolveTestProperties(C context, MicronautTestValue testAnnotationValue, Map<String, Object> testProperties) Resolves any test properties.- Parameters:
context- The test contexttestAnnotationValue- The test annotationtestProperties- The test properties
-
beforeEach
protected void beforeEach(C context, @Nullable Object testInstance, @Nullable AnnotatedElement method, List<io.micronaut.context.annotation.Property> propertyAnnotations) To be called by the different implementations before each test method.- Parameters:
context- The test contexttestInstance- The test instancemethod- The test methodpropertyAnnotations- The@Propertyannotations found in the test method, if any
-
injectTestInstance
Injects an instance of the test class, or of a class nested in it, from the running context.- Parameters:
testInstance- The test instance
-
afterClass
Executed after the class is complete.- Parameters:
context- the context
-
afterEach
-
startApplicationContext
protected void startApplicationContext()Starts the application context. -
applicationContextRebuilt
Called afterrebuildContext = truereplaced the application context before a test, once the new context, and its embedded application if any, have started. Subclasses that keep state from the previous context, beyond what this class resolves again, refresh it here.- Parameters:
context- The test context- Since:
- 5.2.1
-
isTestSuiteBeanPresent
- Parameters:
requiredTestClass- The test class- Returns:
- true if the te given class has a bean definition class in the classpath (ie: the annotation processor has been run correctly)
-
isNativeImageExecution
protected boolean isNativeImageExecution()Whether the tests run inside a native image, where generated bean definition classes cannot be looked up by name. Override to change how a native image is detected.- Returns:
- true if running inside a native image
-
alignMocks
-
beforeInjectTestInstance
-