Package ru.vyarus.dropwizard.guice.test
Class TestSupport
- java.lang.Object
-
- ru.vyarus.dropwizard.guice.test.TestSupport
-
public final class TestSupport extends java.lang.ObjectUtility class combining test-framework agnostic utilities.DropwizardTestSupportfactoryGuiceyTestSupportfactory (same as previous but without web part starting)ClientSupportfactory (web client)- Guice-related utilities like
Injectoror 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 interfaceTestSupport.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>
GuiceyTestSupport<C>coreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath)CreatesGuiceyTestSupportinstance for application configured from configuration file.static voiddebugExtensions()Enables debug output for registered junit 5 extensions.static <T> TgetBean(io.dropwizard.testing.DropwizardTestSupport<?> support, com.google.inject.Key<T> key)Shortcut for accessing guice beans.static <T> TgetBean(io.dropwizard.testing.DropwizardTestSupport<?> support, java.lang.Class<T> type)Shortcut for accessing guice beans.static com.google.inject.InjectorgetInjector(io.dropwizard.testing.DropwizardTestSupport<?> support)static voidinjectBeans(io.dropwizard.testing.DropwizardTestSupport<?> support, java.lang.Object target)Shortcut method to apply field injections into target object instance.static <T> Trun(io.dropwizard.testing.DropwizardTestSupport<?> support, TestSupport.RunCallback<T> callback)Normally,DropwizardTestSupport.before()andDropwizardTestSupport.after()methods are called separately.static <C extends io.dropwizard.Configuration>
voidrunCoreApp(java.lang.Class<? extends io.dropwizard.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.Configuration>
TrunCoreApp(java.lang.Class<? extends io.dropwizard.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.Configuration>
voidrunWebApp(java.lang.Class<? extends io.dropwizard.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.Configuration>
TrunWebApp(java.lang.Class<? extends io.dropwizard.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.Configuration>
io.dropwizard.testing.DropwizardTestSupport<C>webApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath)CreatesDropwizardTestSupportinstance for application configured from configuration file.static ClientSupportwebClient(io.dropwizard.testing.DropwizardTestSupport<?> support)Factory method for creating helper web client.
-
-
-
Method Detail
-
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)CreatesDropwizardTestSupportinstance for application configured from configuration file.DropwizardTestSupportstarts 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.Configuration> GuiceyTestSupport<C> coreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath)
CreatesGuiceyTestSupportinstance for application configured from configuration file. It is pre-configuredDropwizardTestSupportinstance (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.ExceptionNormally,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.Configuration> void runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath) throws java.lang.ExceptionShortcut 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.Configuration> T runWebApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, @Nullable TestSupport.RunCallback<T> callback) throws java.lang.ExceptionShortcut 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.Configuration> void runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath) throws java.lang.ExceptionShortcut 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.Configuration> T runCoreApp(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, @Nullable java.lang.String configPath, @Nullable TestSupport.RunCallback<T> callback) throws java.lang.ExceptionShortcut 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.
-
-