Class GuiceyExtensionsSupport
- 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.ParameterResolver,org.junit.jupiter.api.extension.TestInstantiationAwareExtension
- Direct Known Subclasses:
TestDropwizardAppExtension,TestGuiceyAppExtension
DropwizardTestSupport object
for actual execution (only configuration differs).
Extensions might be used on class level (annotation and manual registration in static field; when extension start dropwizard app before all tests and shut down it after all tests) and on method level (manual registration in non static field; application starts before each test).
Nested tests also supported.
Test instance is not managed by guice! Only Injector.injectMembers(Object) applied
for it to process test fields injection. Guice AOP can't be used on test methods. Technically, creating test
instances with guice is possible, but in this case nested tests could not work at all, which is unacceptable.
Extension detects static fields of GuiceyConfigurationHook type, annotated with EnableHook
and initialize these hooks automatically. It was done like this to simplify customizations, when main extension
could be declared as annotation and hook as field. Also, it was impossible to implement hooks support
with junit extension. Hook field could be declared even in base test class.
Also, detects TestEnvironmentSetup fields annotated with
EnableSetup. Behaviour is the same as with hook fields.
For external integrations (other extensions), there is a special "hack" allowing to access
DropwizardTestSupport object (and so get access to injector): lookupSupport(ExtensionContext).
And shortcuts lookupInjector(ExtensionContext) and lookupClient(ExtensionContext).
- Since:
- 29.04.2020
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ListenersSupportEvent listeners support.protected final TestExtensionsTrackerExtensions registration tracker. -
Constructor Summary
ConstructorsConstructorDescriptionCreate extensions support. -
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(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 booleancloseReusableApp(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Close global application instance.protected ClientSupportgetClient(org.junit.jupiter.api.extension.ExtensionContext extensionContext) protected abstract ExtensionConfiggetConfig(org.junit.jupiter.api.extension.ExtensionContext context) Returns existing config or parse it from annotation.protected static org.junit.jupiter.api.extension.ExtensionContext.StoregetExtensionStore(org.junit.jupiter.api.extension.ExtensionContext context) protected Optional<com.google.inject.Injector>getInjector(org.junit.jupiter.api.extension.ExtensionContext extensionContext) protected io.dropwizard.testing.DropwizardTestSupport<?>getSupport(org.junit.jupiter.api.extension.ExtensionContext extensionContext) static booleanisReusableAppUsed(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Shortcut for testing if current test is using reusable application instead of test-specific application instance.static Optional<ClientSupport>lookupClient(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Shortcut forClientSupportobject lookup by other extensions.static Optional<com.google.inject.Injector>lookupInjector(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Lookup test-specific injector.static <C extends io.dropwizard.core.Configuration>
Optional<io.dropwizard.testing.DropwizardTestSupport<C>>lookupSupport(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Static "hack" for other extensions extending base guicey extensions abilities.protected abstract io.dropwizard.testing.DropwizardTestSupport<?>prepareTestSupport(String configPrefix, org.junit.jupiter.api.extension.ExtensionContext context, List<TestEnvironmentSetup> setups) The only role of actual extension class is to configureDropwizardTestSupportobject according to annotation.Methods inherited from class ru.vyarus.dropwizard.guice.test.jupiter.ext.TestParametersSupport
resolveParameter, supportsParameterMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Field Details
-
tracker
Extensions registration tracker. -
listeners
Event listeners support.
-
-
Constructor Details
-
GuiceyExtensionsSupport
Create extensions support.- Parameters:
tracker- tracker
-
-
Method Details
-
beforeAll
- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Throws:
Exception
-
beforeEach
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
Exception
-
afterEach
- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Throws:
Exception
-
afterAll
- Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback- Throws:
Exception
-
lookupSupport
public static <C extends io.dropwizard.core.Configuration> Optional<io.dropwizard.testing.DropwizardTestSupport<C>> lookupSupport(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Static "hack" for other extensions extending base guicey extensions abilities.The only thin moment here is extensions order! Junit preserve declaration order so in most cases it should not be a problem.
- Type Parameters:
C- configuration type- Parameters:
extensionContext- extension context- Returns:
- dropwizard support object prepared by guicey extension, or empty optional if no guicey extension used or its beforeAll hook was not called yet
-
lookupInjector
public static Optional<com.google.inject.Injector> lookupInjector(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Lookup test-specific injector.InjectorLookupmechanism not used here because it does not provide injector after application stop, which is not very usable for tests.- Parameters:
extensionContext- extension context- Returns:
- application injector or empty optional
-
lookupClient
public static Optional<ClientSupport> lookupClient(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Shortcut forClientSupportobject lookup by other extensions.Custom extension must be activated after main guicey extension!
- Parameters:
extensionContext- extension context- Returns:
- client factory object or empty optional
-
isReusableAppUsed
public static boolean isReusableAppUsed(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Shortcut for testing if current test is using reusable application instead of test-specific application instance.- Parameters:
extensionContext- extension context- Returns:
- true if global application instance used, false otherwise
-
closeReusableApp
public static boolean closeReusableApp(org.junit.jupiter.api.extension.ExtensionContext extensionContext) Close global application instance. Do nothing if no global application registered for provided base class.Method exists to allow creation of custom extension like "@CloseAppAfterTest" to be able to close app at some points (next test would start a fresh app again).
In order to close application before test use
ReusableAppUtilsdirectly (it must be called before extension (which should start new app instance) and so there is no base class in context yet.- Parameters:
extensionContext- extension context- Returns:
- true if app was closed, false otherwise
-
getConfig
protected abstract ExtensionConfig getConfig(org.junit.jupiter.api.extension.ExtensionContext context) Returns existing config or parse it from annotation.Separate configuration creation is important for application re-use logic when additional actions from
prepareTestSupport(String, org.junit.jupiter.api.extension.ExtensionContext, java.util.List)should be omitted in case if context already created.- Parameters:
context- extension context- Returns:
- extension configuration
-
prepareTestSupport
protected abstract io.dropwizard.testing.DropwizardTestSupport<?> prepareTestSupport(String configPrefix, org.junit.jupiter.api.extension.ExtensionContext context, List<TestEnvironmentSetup> setups) The only role of actual extension class is to configureDropwizardTestSupportobject according to annotation.- Parameters:
configPrefix- configuration properties prefixcontext- extension contextsetups- setup extensions resolved from fields (or empty list)- Returns:
- configured dropwizard test support object
-
getSupport
protected io.dropwizard.testing.DropwizardTestSupport<?> getSupport(org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
getSupportin classTestParametersSupport- Parameters:
extensionContext- junit extension context- Returns:
- dropwizard test support object assigned to test instance or null
-
getClient
protected ClientSupport getClient(org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
getClientin classTestParametersSupport- Parameters:
extensionContext- junit extension context- Returns:
- client factory object assigned to test instance or null
-
getInjector
protected Optional<com.google.inject.Injector> getInjector(org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
getInjectorin classTestParametersSupport- Parameters:
extensionContext- junit extension context- Returns:
- application injector or null
-
getExtensionStore
protected static org.junit.jupiter.api.extension.ExtensionContext.Store getExtensionStore(org.junit.jupiter.api.extension.ExtensionContext context) - Parameters:
context- junit context- Returns:
- extension-specific store
-