Class GuiceyExtensionsSupport
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.jupiter.ext.TestParametersSupport
-
- ru.vyarus.dropwizard.guice.test.jupiter.ext.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
- Direct Known Subclasses:
TestDropwizardAppExtension
,TestGuiceyAppExtension
public abstract class GuiceyExtensionsSupport extends TestParametersSupport 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
Base class for junit 5 extensions implementations. All extensions useDropwizardTestSupport
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 withEnableHook
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 withEnableSetup
. 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 shortcutslookupInjector(ExtensionContext)
andlookupClient(ExtensionContext)
.- Since:
- 29.04.2020
- See Also:
for supported test parameters
-
-
Field Summary
Fields Modifier and Type Field Description protected TestExtensionsTracker
tracker
-
Constructor Summary
Constructors Constructor Description GuiceyExtensionsSupport(TestExtensionsTracker tracker)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
void
afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
void
beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
void
beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
protected ClientSupport
getClient(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
protected static org.junit.jupiter.api.extension.ExtensionContext.Store
getExtensionStore(org.junit.jupiter.api.extension.ExtensionContext context)
protected java.util.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 java.util.Optional<ClientSupport>
lookupClient(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Shortcut forClientSupport
object lookup by other extensions.static java.util.Optional<com.google.inject.Injector>
lookupInjector(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Shortcut for application injector resolution be used by other extensions.static java.util.Optional<io.dropwizard.testing.DropwizardTestSupport<?>>
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(java.lang.String configPrefix, org.junit.jupiter.api.extension.ExtensionContext context, java.util.List<TestEnvironmentSetup> setups)
The only role of actual extension class is to configureDropwizardTestSupport
object according to annotation.-
Methods inherited from class ru.vyarus.dropwizard.guice.test.jupiter.ext.TestParametersSupport
resolveParameter, supportsParameter
-
-
-
-
Field Detail
-
tracker
protected final TestExtensionsTracker tracker
-
-
Constructor Detail
-
GuiceyExtensionsSupport
public GuiceyExtensionsSupport(TestExtensionsTracker tracker)
-
-
Method Detail
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) throws java.lang.Exception
- Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
- Throws:
java.lang.Exception
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws java.lang.Exception
- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
java.lang.Exception
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) throws java.lang.Exception
- Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
- Throws:
java.lang.Exception
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) throws java.lang.Exception
- Specified by:
afterAll
in interfaceorg.junit.jupiter.api.extension.AfterAllCallback
- Throws:
java.lang.Exception
-
lookupSupport
public static java.util.Optional<io.dropwizard.testing.DropwizardTestSupport<?>> 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.
- Parameters:
extensionContext
- extension context- Returns:
- dropwizard support object prepared by guicey extension, or null if no guicey extension used or its beforeAll hook was not called yet
-
lookupInjector
public static java.util.Optional<com.google.inject.Injector> lookupInjector(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Shortcut for application injector resolution be used by other extensions.Custom extension must be activated after main guicey extension!
- Parameters:
extensionContext
- extension context- Returns:
- application injector or null if not available
-
lookupClient
public static java.util.Optional<ClientSupport> lookupClient(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
Shortcut forClientSupport
object lookup by other extensions.Custom extension must be activated after main guicey extension!
- Parameters:
extensionContext
- extension context- Returns:
- client factory object or null if not available
-
prepareTestSupport
protected abstract io.dropwizard.testing.DropwizardTestSupport<?> prepareTestSupport(java.lang.String configPrefix, org.junit.jupiter.api.extension.ExtensionContext context, java.util.List<TestEnvironmentSetup> setups)
The only role of actual extension class is to configureDropwizardTestSupport
object 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:
getSupport
in 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:
getClient
in classTestParametersSupport
- Parameters:
extensionContext
- junit extension context- Returns:
- client factory object assigned to test instance or null
-
getInjector
protected java.util.Optional<com.google.inject.Injector> getInjector(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
- Specified by:
getInjector
in 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)
-
-