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

java.lang.Object
ru.vyarus.dropwizard.guice.test.cmd.CommandTestSupport<C>
Type Parameters:
C - configuration type

public class CommandTestSupport<C extends io.dropwizard.core.Configuration> extends 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 Details

    • applicationClass

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

      protected final String configPath
      Configuration file path.
    • configSourceProvider

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

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

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

      protected final 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 Details

    • CommandTestSupport

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

      public CommandTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, @Nullable io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, @Nullable 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 Details

    • 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:
      IllegalStateException - if called after application startup
    • configModifiers

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

      public CommandResult<C> run(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 String[] input, 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 Exception
      Prepare command execution.
      Parameters:
      preventServerStart - true to avoid server start
      Throws:
      Exception - on error
    • reset

      protected void reset()
      Reset system state after execution.