Class ExtensionBuilder<K extends io.dropwizard.core.Configuration,T extends ExtensionBuilder<K,T,C>,C extends ExtensionConfig>
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.jupiter.ext.conf.ExtensionBuilder<K,T,C>
-
- Type Parameters:
K- dropwizard configuration typeC- config object typeT- builder type
- Direct Known Subclasses:
TestDropwizardAppExtension.Builder,TestExtension,TestGuiceyAppExtension.Builder
public abstract class ExtensionBuilder<K extends io.dropwizard.core.Configuration,T extends ExtensionBuilder<K,T,C>,C extends ExtensionConfig> extends java.lang.ObjectBase class forTestDropwizardAppExtension,TestGuiceyAppExtensionandTestEnvironmentSetupbuilders (to avoid duplicating method implementations).- Since:
- 12.05.2022
-
-
Constructor Summary
Constructors Constructor Description ExtensionBuilder(C cfg)Create builder.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TclientFactory(TestClientFactory factory)Use custom jersey client builder forClientSupportobject.Tconfig(org.junit.jupiter.api.function.ThrowingSupplier<K> configProvider)Specify configuration instance directly, instead of parsing yaml file.TconfigModifiers(java.lang.Class<? extends ConfigModifier<? extends io.dropwizard.core.Configuration>>... modifiers)Configuration modifier is an alternative for configuration override, which is limited for simple property types (for example, a collection could not be overridden).<P extends K>
TconfigModifiers(ConfigModifier<P>... modifiers)Configuration modifier is an alternative for configuration override, which is limited for simple property types (for example, a collection could not be overridden).TconfigOverride(java.lang.String key, java.lang.String value)Shortcut for config override registration.TconfigOverride(java.lang.String key, java.util.function.Supplier<java.lang.String> supplier)Register config override with a supplier.TconfigOverrideByExtension(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.TconfigOverrideByExtension(org.junit.jupiter.api.extension.ExtensionContext.Namespace namespace, java.lang.String storageKey, java.lang.String configPath)Override configuration value from 3rd party junit extension.TconfigOverrides(java.lang.String... values)Specifies configuration overrides pairs in format:"key: value".<K extends io.dropwizard.testing.ConfigOverride & ConfigurablePrefix>
TconfigOverrides(K... values)DirectConfigOverrideobjects support.Tdebug()Enables debug output for extension: used setup objects, hooks and applied config overrides.TdisableDefaultExtensions()Thooks(java.lang.Class<? extends GuiceyConfigurationHook>... hooks)Hooks provide access to guice builder allowing application-level customization of application context in tests.Thooks(GuiceyConfigurationHook... hooks)May be used for quick configurations with lambda:TinjectOnce()When test lifecycle isTestInstance.Lifecycle.PER_CLASSsame test instance used for all test methods.TreuseApplication()By default, a new application instance is started for each test.Twith(org.junit.jupiter.api.function.ThrowingConsumer<T> action)Custom block to perform manual configurations inside.
-
-
-
Field Detail
-
cfg
protected final C extends ExtensionConfig cfg
Configuration instance.
-
-
Constructor Detail
-
ExtensionBuilder
public ExtensionBuilder(C cfg)
Create builder.- Parameters:
cfg- configuration instance
-
-
Method Detail
-
with
public T with(org.junit.jupiter.api.function.ThrowingConsumer<T> action)
Custom block to perform manual configurations inside. It is better suited for lambda configurations (when builder configured in test field). Also, it captures exceptions (no need for manual try-catch blocks).- Parameters:
action- action to execute- Returns:
- builder instance for chained calls
-
config
public T config(org.junit.jupiter.api.function.ThrowingSupplier<K> configProvider)
Specify configuration instance directly, instead of parsing yaml file.NOTE: Configuration overrides will not work! But configuration modifiers will work.
- Parameters:
configProvider- configuration instance provider- Returns:
- builder instance for chained calls
-
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
DropwizardTestSupportholds overrides inSet).- Parameters:
values- overriding configuration values in "key: value" format- Returns:
- builder instance for chained calls
- See Also:
for using objects directly,configModifiers(ru.vyarus.dropwizard.guice.test.util.ConfigModifier[])
-
configOverrides
@SafeVarargs public final <K extends io.dropwizard.testing.ConfigOverride & ConfigurablePrefix> T configOverrides(K... values)
DirectConfigOverrideobjects 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
ConfigurablePrefixinterface 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,configModifiers(ru.vyarus.dropwizard.guice.test.util.ConfigModifier[])
-
configOverride
public T configOverride(java.lang.String key, java.lang.String value)
Shortcut for config override registration.Note that overrides order is not predictable so don't specify multiple values for the same property (see
DropwizardTestSupportholds overrides inSet).- Parameters:
key- property namevalue- property value- Returns:
- builder instance for chained calls
-
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
DropwizardTestSupportholds overrides inSet).- Parameters:
key- configuration keysupplier- value supplier- Returns:
- builder instance for chained calls
- See Also:
configModifiers(ru.vyarus.dropwizard.guice.test.util.ConfigModifier[])
-
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
- See Also:
configModifiers(ru.vyarus.dropwizard.guice.test.util.ConfigModifier[])
-
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 inBeforeAllCallbackbecause 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
TestEnvironmentSetupimplementation, 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
- See Also:
configModifiers(ru.vyarus.dropwizard.guice.test.util.ConfigModifier[])
-
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
-
configModifiers
@SafeVarargs public final <P extends K> T configModifiers(ConfigModifier<P>... modifiers)
Configuration modifier is an alternative for configuration override, which is limited for simple property types (for example, a collection could not be overridden).Modifier is called before application run phase. Only logger configuration is applied at this moment (and so you can't change it). Modifier would work with both yaml and instance-based configurations.
Method supposed to be used with lambdas and so limited for application configuration class. For generic configurations (based on configuration subclass or raw
Configuration) useconfigModifiers(Class[]).- Type Parameters:
P- configuration type- Parameters:
modifiers- configuration modifiers- Returns:
- builder instance for chained calls
-
configModifiers
@SafeVarargs public final T configModifiers(java.lang.Class<? extends ConfigModifier<? extends io.dropwizard.core.Configuration>>... modifiers)
Configuration modifier is an alternative for configuration override, which is limited for simple property types (for example, a collection could not be overridden).Modifier is called before application run phase. Only logger configuration is applied at this moment (and so you can't change it). Modifier would work with both yaml and instance-based configurations.
Method is useful for generic modifiers (based on configuration subclass or raw
Configuration).- Parameters:
modifiers- configuration modifiers- Returns:
- builder instance for chained calls
-
injectOnce
public T injectOnce()
When test lifecycle isTestInstance.Lifecycle.PER_CLASSsame test instance used for all test methods. By default, guicey would perform fields injection before each method because there might be prototype beans that must be refreshed for each test method. If you don't rely on prototypes, injections could be performed just once (for the first test method).- 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).Also, shows guicey extension time, so if you suspect that guicey spent too much time, use the debug option to be sure. Performance report is published after each "before each" phase and after "after all" to let you see how extension time increased with each test method (for non-static guicey extension (executed per method), performance printed after "before each" and "after each" because before/after all not available)
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, a 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 the 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
-
disableDefaultExtensions
public T disableDefaultExtensions()
Default extensions:MockBean,StubBean,SpyBean,TrackBean,RecordLogs,StubRest.Disables service lookup for
TestEnvironmentSetup.By default, these extensions enabled and this option could disable them (if there are problems with them or fields analysis took too much time).
- Returns:
- builder instance for chained calls
-
clientFactory
public T clientFactory(TestClientFactory factory)
Use custom jersey client builder forClientSupportobject.- Parameters:
factory- factory implementation- Returns:
- builder instance for chained calls
-
-