Class ExtensionBuilder<K extends io.dropwizard.core.Configuration,​T extends ExtensionBuilder<K,​T,​C>,​C extends ExtensionConfig>

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected C cfg
      Configuration instance.
    • Constructor Summary

      Constructors 
      Constructor Description
      ExtensionBuilder​(C cfg)
      Create builder.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T clientFactory​(TestClientFactory factory)
      Use custom jersey client builder for ClientSupport object.
      T config​(org.junit.jupiter.api.function.ThrowingSupplier<K> configProvider)
      Specify configuration instance directly, instead of parsing yaml file.
      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).
      <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).
      T configOverride​(java.lang.String key, java.lang.String value)
      Shortcut for config override registration.
      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 for configOverrideByExtension( 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>
      T
      configOverrides​(K... values)
      Direct ConfigOverride objects support.
      T debug()
      Enables debug output for extension: used setup objects, hooks and applied config overrides.
      T disableDefaultExtensions()
      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 injectOnce()
      When test lifecycle is TestInstance.Lifecycle.PER_CLASS same test instance used for all test methods.
      T reuseApplication()
      By default, a new application instance is started for each test.
      T with​(org.junit.jupiter.api.function.ThrowingConsumer<T> action)
      Custom block to perform manual configurations inside.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 DropwizardTestSupport holds overrides in Set).

        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[])
      • 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 DropwizardTestSupport holds overrides in Set).

        Parameters:
        key - property name
        value - 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 DropwizardTestSupport holds overrides in Set).

        Parameters:
        key - configuration key
        supplier - 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 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 use ExtensionContext.Namespace.GLOBAL). It is advised to use the same storage key as configuration path (for simplicity). Value must be initialized in BeforeAllCallback 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 in
        storageKey - value name in namespace
        configPath - 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:
        
         .hooks(builder -> builder.modules(new DebugModule()))
         
        May be called multiple times (values appended).

        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 KT 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) use configModifiers(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 is TestInstance.Lifecycle.PER_CLASS same 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 for ClientSupport object.
        Parameters:
        factory - factory implementation
        Returns:
        builder instance for chained calls