Class CommandTestSupport<C extends io.dropwizard.core.Configuration>
- Type Parameters:
C- configuration type
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
FieldsModifier and TypeFieldDescriptionprotected io.dropwizard.core.Application<C>Application instance.Application class.protected io.dropwizard.core.setup.Bootstrap<C>Bootstrap instance.protected final Set<io.dropwizard.testing.ConfigOverride>Configuration overrides.protected final StringConfiguration file path.protected final io.dropwizard.configuration.ConfigurationSourceProviderConfiguration source provider.protected CConfiguration instance (used instead of file).protected final StringConfiguration overrides prefix.protected io.dropwizard.core.setup.EnvironmentEnvironment instance.protected final booleanFlag that indicates whether instance was constructed with an explicit Configuration object or not; handling of the two cases differ.protected com.google.inject.InjectorInjector instance.protected final List<ConfigModifier<C>>Configuration modifiers. -
Constructor Summary
ConstructorsConstructorDescriptionCommandTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration) Create a support object.CommandTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides) Create a support object. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbefore(boolean preventServerStart) Prepare command execution.configModifiers(List<ConfigModifier<C>> modifiers) Register configuration modifiers.final CommandTestSupport<C>configModifiers(ConfigModifier<C>... modifier) Register configuration modifiers.protected voidreset()Reset system state after execution.Execute dropwizard command.Run for commands requiring console user input (in all other aspects is the same asrun(String...)).
-
Field Details
-
applicationClass
protected final Class<? extends io.dropwizard.core.Application<C extends io.dropwizard.core.Configuration>> applicationClassApplication class. -
configPath
Configuration file path. -
configSourceProvider
protected final io.dropwizard.configuration.ConfigurationSourceProvider configSourceProviderConfiguration source provider. -
configOverrides
Configuration overrides. -
modifiers
Configuration modifiers. -
customPropertyPrefix
Configuration overrides prefix. -
explicitConfig
protected final boolean explicitConfigFlag that indicates whether instance was constructed with an explicit Configuration object or not; handling of the two cases differ. Needed because state ofconfigurationchanges during lifecycle. -
configuration
Configuration instance (used instead of file). -
environment
protected io.dropwizard.core.setup.Environment environmentEnvironment instance. -
injector
protected com.google.inject.Injector injectorInjector instance. -
application
Application instance. -
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 classconfiguration- 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 classconfigPath- configuration file pathconfigSourceProvider- configuration source providercustomPropertyPrefix- config overrides prefixconfigOverrides- config overrides
-
-
Method Details
-
configModifiers
Register configuration modifiers.- Parameters:
modifier- configuration modifiers- Returns:
- command support instance for chained calls
- Throws:
IllegalStateException- if called after application startup
-
configModifiers
Register configuration modifiers.- Parameters:
modifiers- configuration modifiers- Returns:
- command support instance for chained calls
- Throws:
IllegalStateException- if called after application startup
-
run
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
Run for commands requiring console user input (in all other aspects is the same asrun(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 askargs- command run arguments- Returns:
- command execution result
-
before
Prepare command execution.- Parameters:
preventServerStart- true to avoid server start- Throws:
Exception- on error
-
reset
protected void reset()Reset system state after execution.
-