Interface TestEnvironmentSetup

  • Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface TestEnvironmentSetup
    Extension for guicey junit 5 test extensions (TestDropwizardApp and TestGuiceyApp). Called before test application execution. Useful for management of additional environment objects like embedded database and overriding test application configuration. Consider this as a simpler option to writing custom junit extensions.

    If you need to take action after test execution (e.g. shutdown database) then return AutoCloseable or ExtensionContext.Store.CloseableResource object, and it would be closed automatically.

    The same could be achieved with an additional junit 5 extensions, but it might be harder to properly synchronize lifecycles (extensions order would be important). Environment support assumed to be a simpler alternative.

    Setup object might be registered directly into extension annotation or with extension builder (when extension registered with field). Also, support object may be declared in field (in test or any base test class), annotated with EnableSetup annotation (annotation is required to provide context javadoc).

    To avoid confusion with guicey hooks: setup object required to prepare test environment before test (and apply required configurations) whereas hooks is a general mechanism for application customization (not only in tests).

    Since:
    12.05.2022
    • Method Detail

      • setup

        java.lang.Object setup​(TestExtension extension)
        Called before test application startup under junit "before all" phase. Assumed to be used for starting additional test objects (like embedded database) and application configuration (configuration overrides). Provided object allow you to provide direct configuration overrides (e.g. to override database credentials).

        For simplicity, any non closable returned object simply ignored. This was done to simplify lambas usage: TestEnvironmentSetup env = ext -> ext.configOverrides("foo:1") - here configuration object would be implicitly returned (because all methods return object itself for chained calls) and ignored.

        Parameters:
        extension - test extension configuration object (support chained calls)
        Returns:
        AutoCloseable or ExtensionContext.Store.CloseableResource if something needs to be shut down after test, any other object would be ignored (including null)