Class TestSupport
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.TestSupport
-
public final class TestSupport extends java.lang.Object
Utility class combining test-framework agnostic utilities.DropwizardTestSupport
factoryGuiceyTestSupport
factory (same as previous but without web part starting)ClientSupport
factory (web client)- Guice-related utilities like
Injector
or beans lookup - Utility methods for running before and after methods in one call (useful for error situation testing).
- Utilities for accessing context
DropwizardTestSupport
object for both manually running tests (with run method below) or junit extensions
- Since:
- 09.02.2022
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
TestSupport.OutputCallback
Callback forcaptureOutput(ru.vyarus.dropwizard.guice.test.TestSupport.OutputCallback)
method.static interface
TestSupport.RunCallback<T>
Callback interface used for utility run application methods inTestSupport
.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <C extends io.dropwizard.Configuration>
TestSupportBuilder<C>build(java.lang.Class<? extends io.dropwizard.Application<C>> app)
Generic builder to build and run application (core or web).static <C extends io.dropwizard.Configuration>
CommandRunBuilder<C>buildCommandRunner(java.lang.Class<? extends io.dropwizard.Application<C>> app)
Builder (similar tobuild(Class)
) for testing commands.static java.lang.String
captureOutput(TestSupport.OutputCallback action)
Simple utility to capture console output.static <C extends io.dropwizard.Configuration>
GuiceyTestSupport<C>coreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, java.lang.String... overrides)
CreatesGuiceyTestSupport
instance for application configured from configuration file.static void
debugExtensions()
Enables debug output for registered junit 5 extensions.static <T> T
getBean(io.dropwizard.testing.DropwizardTestSupport<?> support, com.google.inject.Key<T> key)
Shortcut for accessing guice beans.static <T> T
getBean(io.dropwizard.testing.DropwizardTestSupport<?> support, java.lang.Class<T> type)
Shortcut for accessing guice beans.static <C extends io.dropwizard.Configuration>
io.dropwizard.testing.DropwizardTestSupport<C>getContext()
Obtains a context support object used by test application running in the current thread.static ClientSupport
getContextClient()
Obtains a context client instance used by test application running in the current thread.static com.google.inject.Injector
getInjector(io.dropwizard.testing.DropwizardTestSupport<?> support)
static void
injectBeans(io.dropwizard.testing.DropwizardTestSupport<?> support, java.lang.Object target)
Shortcut method to apply field injections into target object instance.static <T> T
run(io.dropwizard.testing.DropwizardTestSupport<?> support, TestClientFactory clientFactory, TestSupport.RunCallback<T> callback)
Normally,DropwizardTestSupport.before()
andDropwizardTestSupport.after()
methods are called separately.static <T> T
run(io.dropwizard.testing.DropwizardTestSupport<?> support, TestSupport.RunCallback<T> callback)
static <C extends io.dropwizard.Configuration>
RunResult<C>run(io.dropwizard.testing.DropwizardTestSupport<C> support)
static <C extends io.dropwizard.Configuration>
RunResult<C>runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass)
Shortcut for core application startup.static <C extends io.dropwizard.Configuration>
RunResult<C>runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, java.lang.String... overrides)
Shortcut for core application startup test (replacingTestSupport.execute(TestSupport.coreApp(App.class, path))
).static <T,C extends io.dropwizard.Configuration>
TrunCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, TestSupport.RunCallback<T> callback, java.lang.String... overrides)
Shortcut for core application startup test (replacingTestSupport.execute(TestSupport.coreApp(App.class, path), callback)
).static <T,C extends io.dropwizard.Configuration>
TrunCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, TestSupport.RunCallback<T> callback)
Shortcut for core application startup.static <C extends io.dropwizard.Configuration>
RunResult<C>runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass)
Shortcut for web application startup.static <C extends io.dropwizard.Configuration>
RunResult<C>runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, java.lang.String... overrides)
Shortcut for web application startup with configuration (optional).static <T,C extends io.dropwizard.Configuration>
TrunWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, TestSupport.RunCallback<T> callback, java.lang.String... overrides)
Shortcut for web application startup test (replacingTestSupport.execute(TestSupport.webApp(App.class, path), callback)
).static <T,C extends io.dropwizard.Configuration>
TrunWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, TestSupport.RunCallback<T> callback)
Shortcut for web application startup.static <C extends io.dropwizard.Configuration>
io.dropwizard.testing.DropwizardTestSupport<C>webApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, java.lang.String... overrides)
CreatesDropwizardTestSupport
instance for application configured from configuration file.static ClientSupport
webClient(io.dropwizard.testing.DropwizardTestSupport<?> support)
Factory method for creating a helper web client.static ClientSupport
webClient(io.dropwizard.testing.DropwizardTestSupport<?> support, TestClientFactory factory)
Helper web client creation with custom jersey client factory (to configure client differently).
-
-
-
Method Detail
-
build
public static <C extends io.dropwizard.Configuration> TestSupportBuilder<C> build(java.lang.Class<? extends io.dropwizard.Application<C>> app)
Generic builder to build and run application (core or web). This is the most flexible way to build or run the support object (with all possible options). In simple cases, prefer direct methods likecoreApp(Class, String, String...)
orrunCoreApp(Class, String, String...)
(all these methods are builder shortcuts).Should be useful for testing without a custom test framework integration as it provides lifecycle listener support to simplify setup and cleaup actions.
- Type Parameters:
C
- configuration type- Parameters:
app
- application class- Returns:
- test support object builder
-
buildCommandRunner
public static <C extends io.dropwizard.Configuration> CommandRunBuilder<C> buildCommandRunner(java.lang.Class<? extends io.dropwizard.Application<C>> app)
Builder (similar tobuild(Class)
) for testing commands. In contrast to the application support object, command testing is a one-shot operation. That's why command runner would intercept all used dropwizard objects during execution (including injector, if it was created) for assertions after command shutdown.Could be used to test any command:
Command
,ConfiguredCommand
orEnvironmentCommand
. But injector would be created only in case of environment command. Other objects, likeConfiguration
orEnvironment
might also be absent (if they were not created).Configuration is managed completely the same wat as with
DropwizardTestSupport
object. So there is no need to put a configuration fila path inside command (it would be applied automatically, if provided in builder).Suitable for application startup errors testing: in case of successful startup application would shut down immediately, preventing test freezing.
- Type Parameters:
C
- configuration type- Parameters:
app
- application to test command- Returns:
- builder to configure command execution
-
getContext
public static <C extends io.dropwizard.Configuration> io.dropwizard.testing.DropwizardTestSupport<C> getContext()
Obtains a context support object used by test application running in the current thread. Works for manual runs (using run* methods below) and junit extension runs.- Returns:
- support object running application
- Throws:
java.lang.IllegalStateException
- if context support object is not registered for the current thread
-
getContextClient
public static ClientSupport getContextClient()
Obtains a context client instance used by test application running in the current thread. Works for manual runs (using run* methods below) and junit extension runs.- Returns:
- client instance (in case of junit extensions - same instance)
- Throws:
java.lang.IllegalStateException
- if context support object is not registered for the current thread
-
webApp
public static <C extends io.dropwizard.Configuration> io.dropwizard.testing.DropwizardTestSupport<C> webApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, java.lang.String... overrides)
CreatesDropwizardTestSupport
instance for application configured from configuration file.DropwizardTestSupport
starts complete dropwizard application including web part. Suitable for testing rest or servlet endpoints. For web-less application start seecoreApp(Class, String, String...)
.Note: this is just a most common use-case, for more complex cases instantiate object manually using different constructor.
- Type Parameters:
C
- configuration type- Parameters:
appClass
- application classconfigPath
- configuration file path (absolute or relative to working dir) (may be null)overrides
- config override values (in format "path: value")- Returns:
- dropwizard test support instance
-
coreApp
public static <C extends io.dropwizard.Configuration> GuiceyTestSupport<C> coreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, java.lang.String... overrides)
CreatesGuiceyTestSupport
instance for application configured from configuration file. It is pre-configuredDropwizardTestSupport
instance (derivative class) starting only core application part (guice context) without web part. Suitable for testing core logic.Note: this is just a most common use-case, for more complex cases instantiate object manually using different constructor.
- Type Parameters:
C
- configuration type- Parameters:
appClass
- application classconfigPath
- configuration file path (absolute or relative to working dir) (may be null)overrides
- config override values (in format "path: value")- Returns:
- guicey test support instance
-
webClient
public static ClientSupport webClient(io.dropwizard.testing.DropwizardTestSupport<?> support)
Factory method for creating a helper web client. The client is aware of dropwizard configuration and allows easy calling main/rest/admin contexts. It could also be used as a generic web client (for remote endpoints calls).Note that instance must be closed after usage, for example, with try-with-resources:
try(ClientSupport client = TestSupport.webClient(support)) {...}
.- Parameters:
support
- test support object (dropwizard or guicey)- Returns:
- client support instance
-
webClient
public static ClientSupport webClient(io.dropwizard.testing.DropwizardTestSupport<?> support, TestClientFactory factory)
Helper web client creation with custom jersey client factory (to configure client differently). Note thatClientSupport
is still useful in this case because it automatically constructs urls for tested application (based on configuration).- Parameters:
support
- test support object (dropwizard or guicey)factory
- configuration factory- Returns:
- client support instance
-
getInjector
public static com.google.inject.Injector getInjector(io.dropwizard.testing.DropwizardTestSupport<?> support)
- Parameters:
support
- test support object (dropwizard or guicey)- Returns:
- application injector instance
-
getBean
public static <T> T getBean(io.dropwizard.testing.DropwizardTestSupport<?> support, java.lang.Class<T> type)
Shortcut for accessing guice beans.- Type Parameters:
T
- bean type- Parameters:
support
- test support object (dropwizard or guicey)type
- target bean type- Returns:
- bean instance
-
getBean
public static <T> T getBean(io.dropwizard.testing.DropwizardTestSupport<?> support, com.google.inject.Key<T> key)
Shortcut for accessing guice beans.- Type Parameters:
T
- bean type- Parameters:
support
- test support object (dropwizard or guicey)key
- binding key- Returns:
- bean instance
-
injectBeans
public static void injectBeans(io.dropwizard.testing.DropwizardTestSupport<?> support, java.lang.Object target)
Shortcut method to apply field injections into target object instance. Useful to initialize test class fields (under not supported test frameworks).- Parameters:
support
- test support object (dropwizard or guicey)target
- target instance to inject beans
-
run
public static <C extends io.dropwizard.Configuration> RunResult<C> run(io.dropwizard.testing.DropwizardTestSupport<C> support) throws java.lang.Exception
Shortcut forrun(io.dropwizard.testing.DropwizardTestSupport, ru.vyarus.dropwizard.guice.test.TestSupport.RunCallback)
.- Type Parameters:
C
- configuration type- Parameters:
support
- test support instance- Returns:
- result object with the main objects for assertions (for example, to examine configuration)
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
run
public static <T> T run(io.dropwizard.testing.DropwizardTestSupport<?> support, @Nullable TestSupport.RunCallback<T> callback) throws java.lang.Exception
- Type Parameters:
T
- result type- Parameters:
callback
- callback (may be null)support
- test support instance- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
run
public static <T> T run(io.dropwizard.testing.DropwizardTestSupport<?> support, @Nullable TestClientFactory clientFactory, @Nullable TestSupport.RunCallback<T> callback) throws java.lang.Exception
Normally,DropwizardTestSupport.before()
andDropwizardTestSupport.after()
methods are called separately. This method is a shortcut mostly for errors testing whenDropwizardTestSupport.before()
assumed to fail to make sureDropwizardTestSupport.after()
will be called in any case.- Type Parameters:
T
- result type- Parameters:
callback
- callback (may be null)support
- test support instanceclientFactory
- custom client factory forClientSupport
object- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runWebApp
public static <C extends io.dropwizard.Configuration> RunResult<C> runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass) throws java.lang.Exception
Shortcut for web application startup.- Type Parameters:
C
- configuration type- Parameters:
appClass
- application class- Returns:
- result object with the main objects for assertions (for example, to examine configuration)
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runWebApp
public static <T,C extends io.dropwizard.Configuration> T runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable TestSupport.RunCallback<T> callback) throws java.lang.Exception
Shortcut for web application startup.- Type Parameters:
C
- configuration typeT
- result type- Parameters:
appClass
- application classcallback
- callback to execute while application started (may be null)- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runWebApp
public static <C extends io.dropwizard.Configuration> RunResult<C> runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, java.lang.String... overrides) throws java.lang.Exception
Shortcut for web application startup with configuration (optional).- Type Parameters:
C
- configuration type- Parameters:
appClass
- application classconfigPath
- configuration file path (absolute or relative to working dir) (may be null)- Returns:
- test support object used for execution (for example, to examine configuration)
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runWebApp
public static <T,C extends io.dropwizard.Configuration> T runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, @Nullable TestSupport.RunCallback<T> callback, java.lang.String... overrides) throws java.lang.Exception
Shortcut for web application startup test (replacingTestSupport.execute(TestSupport.webApp(App.class, path), callback)
).- Type Parameters:
C
- configuration typeT
- result type- Parameters:
appClass
- application classconfigPath
- configuration file path (absolute or relative to working dir) (may be null)callback
- callback to execute while application started (may be null)overrides
- config override values (in format "path: value")- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runCoreApp
public static <C extends io.dropwizard.Configuration> RunResult<C> runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass) throws java.lang.Exception
Shortcut for core application startup.- Type Parameters:
C
- configuration type- Parameters:
appClass
- application class- Returns:
- result object with the main objects for assertions (for example, to examine configuration)
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runCoreApp
public static <T,C extends io.dropwizard.Configuration> T runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable TestSupport.RunCallback<T> callback) throws java.lang.Exception
Shortcut for core application startup.- Type Parameters:
C
- configuration typeT
- result type- Parameters:
appClass
- application classcallback
- callback to execute while application started (may be null)- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runCoreApp
public static <C extends io.dropwizard.Configuration> RunResult<C> runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, java.lang.String... overrides) throws java.lang.Exception
Shortcut for core application startup test (replacingTestSupport.execute(TestSupport.coreApp(App.class, path))
).- Type Parameters:
C
- configuration type- Parameters:
appClass
- application classconfigPath
- configuration file path (absolute or relative to working dir) (may be null)overrides
- config override values (in format "path: value")- Returns:
- result object with the main objects for assertions (for example, to examine configuration)
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
runCoreApp
public static <T,C extends io.dropwizard.Configuration> T runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, @Nullable TestSupport.RunCallback<T> callback, java.lang.String... overrides) throws java.lang.Exception
Shortcut for core application startup test (replacingTestSupport.execute(TestSupport.coreApp(App.class, path), callback)
).- Type Parameters:
C
- configuration typeT
- result type- Parameters:
appClass
- application classconfigPath
- configuration file path (absolute or relative to working dir) (may be null)callback
- callback to execute while application started (may be null)overrides
- config override values (in format "path: value")- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception (throws may easily be added directly to test method and, without extra exception wrapper, we get exact exceptions as they would be thrown in real application)
-
debugExtensions
public static void debugExtensions()
Enables debug output for registered junit 5 extensions. Simple alias for:System.setProperty("guicey.extensions.debug", "true")
.Alternatively, debug could be enabled on extension directly with debug option.
-
captureOutput
public static java.lang.String captureOutput(TestSupport.OutputCallback action) throws java.lang.Exception
Simple utility to capture console output. Could be used to test application output in some situations.Captured output is duplicated in console (for visual assertions).
Warning: due to System.in/out modification, tests using this method can't run concurrently!
- Parameters:
action
- action to record output- Returns:
- captured output (out + err)
- Throws:
java.lang.Exception
-
-