Class ExtensionBuilder<T extends ExtensionBuilder,C extends ExtensionConfig>
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.jupiter.ext.conf.ExtensionBuilder<T,C>
-
- Type Parameters:
C
- config object typeT
- builder type
- Direct Known Subclasses:
TestDropwizardAppExtension.Builder
,TestExtension
,TestGuiceyAppExtension.Builder
public abstract class ExtensionBuilder<T extends ExtensionBuilder,C extends ExtensionConfig> extends java.lang.Object
Base class forTestDropwizardAppExtension
,TestGuiceyAppExtension
andTestEnvironmentSetup
builders (to avoid duplicating method implementations).- Since:
- 12.05.2022
-
-
Constructor Summary
Constructors Constructor Description ExtensionBuilder(C cfg)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description T
clientFactory(TestClientFactory factory)
Use custom jersey client builder forClientSupport
object.T
configOverride(java.lang.String key, java.util.function.Supplier<java.lang.String> supplier)
Register config override with a supplier.T
configOverrideByExtension(org.junit.jupiter.api.extension.ExtensionContext.Namespace namespace, java.lang.String key)
Shortcut forconfigOverrideByExtension( org.junit.jupiter.api.extension.ExtensionContext.Namespace, String, String)
for cases when storage key and configuration path is the same.T
configOverrideByExtension(org.junit.jupiter.api.extension.ExtensionContext.Namespace namespace, java.lang.String storageKey, java.lang.String configPath)
Override configuration value from 3rd party junit extension.T
configOverrides(java.lang.String... values)
Specifies configuration overrides pairs in format:"key: value"
.<K extends io.dropwizard.testing.ConfigOverride & ConfigurablePrefix>
TconfigOverrides(K... values)
DirectConfigOverride
objects support.T
debug()
Enables debug output for extension: used setup objects, hooks and applied config overrides.T
hooks(java.lang.Class<? extends GuiceyConfigurationHook>... hooks)
Hooks provide access to guice builder allowing application-level customization of application context in tests.T
hooks(GuiceyConfigurationHook... hooks)
May be used for quick configurations with lambda:T
reuseApplication()
By default, new application instance is started for each test.
-
-
-
Field Detail
-
cfg
protected final C extends ExtensionConfig cfg
-
-
Constructor Detail
-
ExtensionBuilder
public ExtensionBuilder(C cfg)
-
-
Method Detail
-
configOverrides
public T configOverrides(java.lang.String... values)
Specifies configuration overrides pairs in format:"key: value"
. Might be called multiple times (values appended).Note that overrides order is not predictable so don't specify multiple values for the same property (see
DropwizardTestSupport
holds overrides inSet
).- Parameters:
values
- overriding configuration values in "key: value" format- Returns:
- builder instance for chained calls
- See Also:
for using objects directly
-
configOverrides
@SafeVarargs public final <K extends io.dropwizard.testing.ConfigOverride & ConfigurablePrefix> T configOverrides(K... values)
DirectConfigOverride
objects support. In most cases, it is simpler to use pure strings withconfigOverrides(String...)
. Direct objects may be useful when provided value must be lazy evaluated (e.g. it is obtained from some other junit extension).IMPORTANT: provided values must implement
ConfigurablePrefix
interface so guicey could insert correct prefix, used by current test (required for parallel tests as all config overrides eventually stored in system properties).May be called multiple times (values appended).
- Type Parameters:
K
- value type- Parameters:
values
- overriding configuration values- Returns:
- builder instance for chained calls
- See Also:
for an exmample of required implementation
,for supplier shortcut
-
configOverride
public T configOverride(java.lang.String key, java.util.function.Supplier<java.lang.String> supplier)
Register config override with a supplier. Useful for values with delayed resolution (e.g. provided by some other extension).Note that overrides order is not predictable so don't specify multiple values for the same property (see
DropwizardTestSupport
holds overrides inSet
).- Parameters:
key
- configuration keysupplier
- value supplier- Returns:
- builder instance for chained calls
-
configOverrideByExtension
public T configOverrideByExtension(org.junit.jupiter.api.extension.ExtensionContext.Namespace namespace, java.lang.String key)
Shortcut forconfigOverrideByExtension( org.junit.jupiter.api.extension.ExtensionContext.Namespace, String, String)
for cases when storage key and configuration path is the same. If possible, prefer this method for simplicity.- Parameters:
namespace
- junit storage namespace to resolve value inkey
- value name in namespace and overriding property name- Returns:
- builder instance for chained calls
-
configOverrideByExtension
public T configOverrideByExtension(org.junit.jupiter.api.extension.ExtensionContext.Namespace namespace, java.lang.String storageKey, java.lang.String configPath)
Override configuration value from 3rd party junit extension. Such value must be stored by extension in the junit store with provided namespace (for simple cases useExtensionContext.Namespace.GLOBAL
). It is advised to use the same storage key as configuration path (for simplicity). Value must be initialized inBeforeAllCallback
because guicey initialize config overrides under this stage.WARNING: keep in mind that your extension must be executed before guicey because otherwise value would not be taken into account. To highlight such cases, guicey would put a warning in logs indicating absent value in configured storage.
Such complication is required for a very special cases when parallel tests execution must be used together with some common extension (for example, starting database) declared in base class with a static field. Using test storage is the only way to guarantee different values in parallel tests.
As an alternative, you can use
TestEnvironmentSetup
implementation, registered directly into guicey extensions (would be called exactly before and after test support object creation and destruction).- Parameters:
namespace
- junit storage namespace to resolve value instorageKey
- value name in namespaceconfigPath
- overriding property name- Returns:
- builder instance for chained calls
-
hooks
@SafeVarargs public final T hooks(java.lang.Class<? extends GuiceyConfigurationHook>... hooks)
Hooks provide access to guice builder allowing application-level customization of application context in tests.Anonymous implementation could be simply declared as field:
@EnableHook static GuiceyConfigurationHook hook = builder -> builder.disableExtension( Something.class)
. Non-static fields may be used only when extension is registered with non-static field (static fields would be also counted in this case). All annotated fields will be detected automatically and objects registered. Fields declared in base test classes are also counted.- Parameters:
hooks
- hook classes to use- Returns:
- builder instance for chained calls
-
hooks
public T hooks(GuiceyConfigurationHook... hooks)
May be used for quick configurations with lambda:
May be called multiple times (values appended)..hooks(builder -> builder.modules(new DebugModule()))
Anonymous implementation could be simply declared as field:
@EnableHook static GuiceyConfigurationHook hook = builder -> builder.disableExtension( Something.class)
. Non-static fields may be used only when extension is registered with non-static field (static fields would be also counted in this case). All annotated fields will be detected automatically and objects registered. Fields declared in base test classes are also counted.- Parameters:
hooks
- hook instances (may be lambdas)- Returns:
- builder instance for chained calls
-
debug
public T debug()
Enables debug output for extension: used setup objects, hooks and applied config overrides. Might be useful for concurrent tests too because each message includes configuration prefix (exactly pointing to context test or method).Configuration overrides are printed after application startup (but before the test) because overridden values are resolved from system properties (applied by
DropwizardTestSupport.before()
). If application startup failed, no configuration overrides would be printed (because dropwizard would immediately clean up system properties). Using system properties is the only way to receive actually applied configuration value because property overrides might be implemented as value providers and potentially return different values.System property might be used to enable debug mode:
-Dguicey.extensions.debug=true
. Or alias in code:TestSupport.debugExtensions()
.- Returns:
- builder instance for chained calls
-
reuseApplication
public T reuseApplication()
By default, new application instance is started for each test. If you want to re-use the same application instance between several tests then put extension declaration in BASE test class and enable reuse option: all tests derived from this base class would use the same application instance.You may have multiple base classes with reusable application declaration (different test hierarchies) - in this case, multiple applications would be kept running during tests execution.
All other extensions (without enabled re-use) will start new applications: take this into account to prevent port clashes with already started reusable apps.
Reused application instance would be stopped after all tests execution.
- Returns:
- builder instance for chained calls
-
clientFactory
public T clientFactory(TestClientFactory factory)
Use custom jersey client builder forClientSupport
object.- Parameters:
factory
- factory implementation- Returns:
- builder instance for chained calls
-
-