Class CommandTestSupport<C extends io.dropwizard.core.Configuration>

  • Type Parameters:
    C - configuration type

    public class CommandTestSupport<C extends io.dropwizard.core.Configuration>
    extends java.lang.Object
    Test helper for running (any) commands. The class is almost similar to DropwizardTestSupport, but differs in a way command is executed: this class use Cli which selects exactly the same command as in real use. Also, command execution is a one-shot operation and so all validations could be performed only after command execution (and not in the middle, as with usual application tests). That's why the resulting object contains all objects used during execution - there is no other way to access them.

    Supposed to be used through builder: TestSupport.buildCommandRunner(Class).

    All types of dropwizard commands are supported, but depending on the command type, some objects in result would be null. Note that guicey could only be used with EnvironmentCommand - for other commands it would be simply ignored (because dropwizard would not call bundle's run method).

    Configuration support is the same as in dropwizard support: config object or configuration file with config overrides might be used. When a configuration file is used, it would be automatically added to called command (as a second argument).

    System in, err and out streams are overridden. To test commands with used input, input strings must be declared before command run. The resulting object would contain complete command output.

    Execution never throws an error: in case of exception, it would be provided inside the resulting object.

    Class is also suitable for application server startup errors check: instead of system exit, it will provide exception in the resulted object. If exception does not appear during startup, test would be failed to prevent infinite run (indicating unexpected successful run).

    Command tests can't be executed in parallel (due to system io overrides)! For junit 5 use @Execution(SAME_THREAD) on test class to prevent concurrent execution.

    Since:
    20.11.2023
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected io.dropwizard.core.Application<C> application
      Application instance.
      protected java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass
      Application class.
      protected io.dropwizard.core.setup.Bootstrap<C> bootstrap
      Bootstrap instance.
      protected java.util.Set<io.dropwizard.testing.ConfigOverride> configOverrides
      Configuration overrides.
      protected java.lang.String configPath
      Configuration file path.
      protected io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider
      Configuration source provider.
      protected C configuration
      Configuration instance (used instead of file).
      protected java.lang.String customPropertyPrefix
      Configuration overrides prefix.
      protected io.dropwizard.core.setup.Environment environment
      Environment instance.
      protected boolean explicitConfig
      Flag that indicates whether instance was constructed with an explicit Configuration object or not; handling of the two cases differ.
      protected com.google.inject.Injector injector
      Injector instance.
      protected java.util.List<ConfigModifier<C>> modifiers
      Configuration modifiers.
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration)
      Create a support object.
      CommandTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, java.lang.String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, java.lang.String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)
      Create a support object.
    • Field Detail

      • applicationClass

        protected final java.lang.Class<? extends io.dropwizard.core.Application<C extends io.dropwizard.core.Configuration>> applicationClass
        Application class.
      • configPath

        protected final java.lang.String configPath
        Configuration file path.
      • configSourceProvider

        protected final io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider
        Configuration source provider.
      • configOverrides

        protected final java.util.Set<io.dropwizard.testing.ConfigOverride> configOverrides
        Configuration overrides.
      • modifiers

        protected final java.util.List<ConfigModifier<C extends io.dropwizard.core.Configuration>> modifiers
        Configuration modifiers.
      • customPropertyPrefix

        protected final java.lang.String customPropertyPrefix
        Configuration overrides prefix.
      • explicitConfig

        protected final boolean explicitConfig
        Flag that indicates whether instance was constructed with an explicit Configuration object or not; handling of the two cases differ. Needed because state of configuration changes during lifecycle.
      • configuration

        protected C extends io.dropwizard.core.Configuration configuration
        Configuration instance (used instead of file).
      • environment

        protected io.dropwizard.core.setup.Environment environment
        Environment instance.
      • injector

        protected com.google.inject.Injector injector
        Injector instance.
      • application

        protected io.dropwizard.core.Application<C extends io.dropwizard.core.Configuration> application
        Application instance.
      • bootstrap

        protected io.dropwizard.core.setup.Bootstrap<C extends io.dropwizard.core.Configuration> bootstrap
        Bootstrap instance.
    • Constructor Detail

      • CommandTestSupport

        public CommandTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                  C configuration)
        Create a support object.
        Parameters:
        applicationClass - application class
        configuration - configuration instance
      • CommandTestSupport

        public CommandTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                  @Nullable
                                  java.lang.String configPath,
                                  @Nullable
                                  io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider,
                                  @Nullable
                                  java.lang.String customPropertyPrefix,
                                  io.dropwizard.testing.ConfigOverride... configOverrides)
        Create a support object.
        Parameters:
        applicationClass - application class
        configPath - configuration file path
        configSourceProvider - configuration source provider
        customPropertyPrefix - config overrides prefix
        configOverrides - config overrides
    • Method Detail

      • configModifiers

        @SafeVarargs
        public final CommandTestSupport<C> configModifiers​(ConfigModifier<C>... modifier)
        Register configuration modifiers.
        Parameters:
        modifier - configuration modifiers
        Returns:
        command support instance for chained calls
        Throws:
        java.lang.IllegalStateException - if called after application startup
      • configModifiers

        public CommandTestSupport<C> configModifiers​(java.util.List<ConfigModifier<C>> modifiers)
        Register configuration modifiers.
        Parameters:
        modifiers - configuration modifiers
        Returns:
        command support instance for chained calls
        Throws:
        java.lang.IllegalStateException - if called after application startup
      • run

        public CommandResult<C> run​(java.lang.String... args)
        Execute dropwizard command. Could be used to execute any command. The only difference with the usual usage is that configuration file should not be declared (as second argument). Config file could be specified - it would not lead to error (if the config file path was not declared in builder also).

        Execution never throws an exception! Any appeared exception would be returned inside an unsuccessful result.

        As it is not possible to run any callback in time of command execution - all runtime objects are provided inside the result for inspection (some objects could be null, depending on a command type).

        All command output would be available in the result. Also, output is streamed to console (to indicate the exact app froze point, if command hangs). Error stream is also available separately to simplify error check.

        Parameters:
        args - command execution arguments (without configuration file)
        Returns:
        command execution result
      • run

        public CommandResult<C> run​(@Nullable
                                    java.lang.String[] input,
                                    java.lang.String... args)
        Run for commands requiring console user input (in all other aspects is the same as run(String...)).

        Error would be thrown if provided responses are not enough (on the first input request, not covered by mock data).

        Parameters:
        input - user input (should be the same (or more) then application would ask
        args - command run arguments
        Returns:
        command execution result
      • before

        protected void before​(boolean preventServerStart)
                       throws java.lang.Exception
        Prepare command execution.
        Parameters:
        preventServerStart - true to avoid server start
        Throws:
        java.lang.Exception - on error
      • reset

        protected void reset()
        Reset system state after execution.