Class TestSupport

java.lang.Object
ru.vyarus.dropwizard.guice.test.TestSupport

public final class TestSupport extends Object
Utility class combining test-framework agnostic utilities.
  • DropwizardTestSupport factory
  • GuiceyTestSupport 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
  • Method Details

    • build

      public static <C extends io.dropwizard.core.Configuration> TestSupportBuilder<C> build(Class<? extends io.dropwizard.core.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 like coreApp(Class, String, String...) or runCoreApp(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.core.Configuration> CommandRunBuilder<C> buildCommandRunner(Class<? extends io.dropwizard.core.Application<C>> app)
      Builder (similar to build(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 or EnvironmentCommand. But injector would be created only in case of environment command. Other objects, like Configuration or Environment 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.core.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.
      Type Parameters:
      C - configuration type
      Returns:
      support object running application
      Throws:
      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:
      IllegalStateException - if context support object is not registered for the current thread
    • webApp

      public static <C extends io.dropwizard.core.Configuration> io.dropwizard.testing.DropwizardTestSupport<C> webApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable String configPath, String... overrides)
      Creates DropwizardTestSupport 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 see coreApp(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 class
      configPath - 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.core.Configuration> GuiceyTestSupport<C> coreApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable String configPath, String... overrides)
      Creates GuiceyTestSupport instance for application configured from configuration file. It is pre-configured DropwizardTestSupport 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 class
      configPath - 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 that ClientSupport 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, 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 Duration injectBeans(io.dropwizard.testing.DropwizardTestSupport<?> support, 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
      Returns:
      injection duration (could be useful for slow injection detection)
    • run

      public static <C extends io.dropwizard.core.Configuration> RunResult<C> run(io.dropwizard.testing.DropwizardTestSupport<C> support) throws Exception
      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:
      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 Exception
      Type Parameters:
      T - result type
      Parameters:
      callback - callback (may be null)
      support - test support instance
      Returns:
      callback result
      Throws:
      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 Exception
      Normally, DropwizardTestSupport.before() and DropwizardTestSupport.after() methods are called separately. This method is a shortcut mostly for errors testing when DropwizardTestSupport.before() assumed to fail to make sure DropwizardTestSupport.after() will be called in any case.
      Type Parameters:
      T - result type
      Parameters:
      callback - callback (may be null)
      support - test support instance
      clientFactory - custom client factory for ClientSupport object
      Returns:
      callback result
      Throws:
      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.core.Configuration> RunResult<C> runWebApp(Class<? extends io.dropwizard.core.Application<C>> appClass) throws 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:
      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.core.Configuration> T runWebApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable TestSupport.RunCallback<T> callback) throws Exception
      Shortcut for web application startup.
      Type Parameters:
      C - configuration type
      T - result type
      Parameters:
      appClass - application class
      callback - callback to execute while application started (may be null)
      Returns:
      callback result
      Throws:
      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.core.Configuration> RunResult<C> runWebApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable String configPath, String... overrides) throws Exception
      Shortcut for web application startup with configuration (optional).
      Type Parameters:
      C - configuration type
      Parameters:
      appClass - application class
      configPath - configuration file path (absolute or relative to working dir) (may be null)
      overrides - config override values (in format "path: value")
      Returns:
      test support object used for execution (for example, to examine configuration)
      Throws:
      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.core.Configuration> T runWebApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable String configPath, @Nullable TestSupport.RunCallback<T> callback, String... overrides) throws Exception
      Shortcut for web application startup test (replacing TestSupport.execute(TestSupport.webApp(App.class, path), callback)).
      Type Parameters:
      C - configuration type
      T - result type
      Parameters:
      appClass - application class
      configPath - 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:
      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.core.Configuration> RunResult<C> runCoreApp(Class<? extends io.dropwizard.core.Application<C>> appClass) throws 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:
      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.core.Configuration> T runCoreApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable TestSupport.RunCallback<T> callback) throws Exception
      Shortcut for core application startup.
      Type Parameters:
      C - configuration type
      T - result type
      Parameters:
      appClass - application class
      callback - callback to execute while application started (may be null)
      Returns:
      callback result
      Throws:
      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.core.Configuration> RunResult<C> runCoreApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable String configPath, String... overrides) throws Exception
      Shortcut for core application startup test (replacing TestSupport.execute(TestSupport.coreApp(App.class, path))).
      Type Parameters:
      C - configuration type
      Parameters:
      appClass - application class
      configPath - 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:
      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.core.Configuration> T runCoreApp(Class<? extends io.dropwizard.core.Application<C>> appClass, @Nullable String configPath, @Nullable TestSupport.RunCallback<T> callback, String... overrides) throws Exception
      Shortcut for core application startup test (replacing TestSupport.execute(TestSupport.coreApp(App.class, path), callback)).
      Type Parameters:
      C - configuration type
      T - result type
      Parameters:
      appClass - application class
      configPath - 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:
      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 String captureOutput(TestSupport.OutputCallback action) throws 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:
      Exception - on action errors (exceptions bypassed)