Package ru.vyarus.dropwizard.guice.test
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).
- Since:
- 09.02.2022
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description 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.core.Configuration>
GuiceyTestSupport<C>coreApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, java.lang.String configPath)
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 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, TestSupport.RunCallback<T> callback)
Normally,DropwizardTestSupport.before()
andDropwizardTestSupport.after()
methods are called separately.static <C extends io.dropwizard.core.Configuration>
voidrunCoreApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, java.lang.String configPath)
Shortcut for core application startup test (replacingTestSupport.execute(TestSupport.coreApp(App.class, path))
).static <T,C extends io.dropwizard.core.Configuration>
TrunCoreApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, java.lang.String configPath, TestSupport.RunCallback<T> callback)
Shortcut for core application startup test (replacingTestSupport.execute(TestSupport.coreApp(App.class, path), callback)
).static <C extends io.dropwizard.core.Configuration>
voidrunWebApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, java.lang.String configPath)
Shortcut for web application startup test (replacingTestSupport.execute(TestSupport.webApp(App.class, path)
).static <T,C extends io.dropwizard.core.Configuration>
TrunWebApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, java.lang.String configPath, TestSupport.RunCallback<T> callback)
Shortcut for web application startup test (replacingTestSupport.execute(TestSupport.webApp(App.class, path), callback)
).static <C extends io.dropwizard.core.Configuration>
io.dropwizard.testing.DropwizardTestSupport<C>webApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, java.lang.String configPath)
CreatesDropwizardTestSupport
instance for application configured from configuration file.static ClientSupport
webClient(io.dropwizard.testing.DropwizardTestSupport<?> support)
Factory method for creating helper web client.
-
-
-
Method Detail
-
webApp
public static <C extends io.dropwizard.core.Configuration> io.dropwizard.testing.DropwizardTestSupport<C> webApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable java.lang.String configPath)
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)
.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)- Returns:
- dropwizard test support instance
-
coreApp
public static <C extends io.dropwizard.core.Configuration> GuiceyTestSupport<C> coreApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable java.lang.String configPath)
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)- Returns:
- guicey test support instance
-
webClient
public static ClientSupport webClient(io.dropwizard.testing.DropwizardTestSupport<?> support)
Factory method for creating helper web client. Client is aware of dropwizard configuration and allows easy calling main/rest/admin contexts. 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
-
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 <T> T run(io.dropwizard.testing.DropwizardTestSupport<?> support, @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 instance- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception
-
runWebApp
public static <C extends io.dropwizard.core.Configuration> void runWebApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable java.lang.String configPath) throws java.lang.Exception
Shortcut for web application startup test (replacingTestSupport.execute(TestSupport.webApp(App.class, path)
).- Type Parameters:
C
- configuration type- Parameters:
appClass
- application classconfigPath
- configuration file path (absolute or relative to working dir) (may be null)- Throws:
java.lang.Exception
- any appeared exception
-
runWebApp
public static <T,C extends io.dropwizard.core.Configuration> T runWebApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable java.lang.String configPath, @Nullable TestSupport.RunCallback<T> callback) 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)- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception
-
runCoreApp
public static <C extends io.dropwizard.core.Configuration> void runCoreApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable java.lang.String configPath) 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)- Throws:
java.lang.Exception
- any appeared exception
-
runCoreApp
public static <T,C extends io.dropwizard.core.Configuration> T runCoreApp(java.lang.Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable java.lang.String configPath, @Nullable TestSupport.RunCallback<T> callback) 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)- Returns:
- callback result
- Throws:
java.lang.Exception
- any appeared exception
-
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.
-
-