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 duffers 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).

    Configurations 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  
      protected java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass  
      protected io.dropwizard.core.setup.Bootstrap<C> bootstrap  
      protected java.util.Set<io.dropwizard.testing.ConfigOverride> configOverrides  
      protected java.lang.String configPath  
      protected io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider  
      protected C configuration  
      protected java.lang.String customPropertyPrefix  
      protected io.dropwizard.core.setup.Environment environment  
      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  
    • Constructor Summary

      Constructors 
      Constructor Description
      CommandTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration)  
      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)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void before​(boolean preventServerStart)  
      protected void reset()  
      CommandResult<C> run​(java.lang.String... args)
      Execute dropwizard command.
      CommandResult<C> run​(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...)).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • applicationClass

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

        protected final java.lang.String configPath
      • configSourceProvider

        protected final io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider
      • configOverrides

        protected final java.util.Set<io.dropwizard.testing.ConfigOverride> configOverrides
      • customPropertyPrefix

        protected final java.lang.String customPropertyPrefix
      • 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
      • environment

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

        protected com.google.inject.Injector injector
      • application

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

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

      • CommandTestSupport

        public CommandTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                  C configuration)
      • 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)
    • Method Detail

      • 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
        Throws:
        java.lang.Exception
      • reset

        protected void reset()