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 type
C - config object type
T - 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 Object
Base class for TestDropwizardAppExtension, TestGuiceyAppExtension and TestEnvironmentSetup builders (to avoid duplicating method implementations).
Since:
12.05.2022
  • Field Details

  • Constructor Details

    • ExtensionBuilder

      public ExtensionBuilder(C cfg)
      Create builder.
      Parameters:
      cfg - configuration instance
  • Method Details

    • 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(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:
    • configOverrides

      @SafeVarargs public final <K extends io.dropwizard.testing.ConfigOverride & ConfigurablePrefix> T configOverrides(K... values)
      Direct ConfigOverride objects support. In most cases, it is simpler to use pure strings with configOverrides(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:
    • configOverride

      public T configOverride(String key, 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(String key, Supplier<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:
    • configOverrideByExtension

      public T configOverrideByExtension(org.junit.jupiter.api.extension.ExtensionContext.Namespace namespace, 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. If possible, prefer this method for simplicity.
      Parameters:
      namespace - junit storage namespace to resolve value in
      key - value name in namespace and overriding property name
      Returns:
      builder instance for chained calls
      See Also:
    • configOverrideByExtension

      public T configOverrideByExtension(org.junit.jupiter.api.extension.ExtensionContext.Namespace namespace, String storageKey, 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:
    • hooks

      @SafeVarargs public final T hooks(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 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) use configModifiers(Class[]).

      Type Parameters:
      P - configuration type
      Parameters:
      modifiers - configuration modifiers
      Returns:
      builder instance for chained calls
    • configModifiers

      @SafeVarargs public final T configModifiers(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
    • apacheClient

      public T apacheClient()
      Shortcut for clientFactory(ru.vyarus.dropwizard.guice.test.client.TestClientFactory) to configure ApacheTestClientFactory. The default HttpUrlConnectorProvider supports only HTTP 1.1 methods and have problem with PATCH method usage on jdk > 16.

      Apache client is not set by default because of its problems with multipart: see jersey issue.

      Returns:
      builder instance for chained calls