Class TestSupport


  • public final class TestSupport
    extends java.lang.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).
    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 in TestSupport.
    • 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)
      Creates GuiceyTestSupport 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() and DropwizardTestSupport.after() methods are called separately.
      static <C extends io.dropwizard.Configuration>
      void
      runCoreApp​(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath)
      Shortcut for core application startup test (replacing TestSupport.execute(TestSupport.coreApp(App.class, path))).
      static <T,​C extends io.dropwizard.Configuration>
      T
      runCoreApp​(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, TestSupport.RunCallback<T> callback)
      Shortcut for core application startup test (replacing TestSupport.execute(TestSupport.coreApp(App.class, path), callback)).
      static <C extends io.dropwizard.Configuration>
      void
      runWebApp​(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath)
      Shortcut for web application startup test (replacing TestSupport.execute(TestSupport.webApp(App.class, path)).
      static <T,​C extends io.dropwizard.Configuration>
      T
      runWebApp​(java.lang.Class<? extends io.dropwizard.Application<C>> appClass, java.lang.String configPath, TestSupport.RunCallback<T> callback)
      Shortcut for web application startup test (replacing TestSupport.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)
      Creates DropwizardTestSupport instance for application configured from configuration file.
      static ClientSupport webClient​(io.dropwizard.testing.DropwizardTestSupport<?> support)
      Factory method for creating helper web client.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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)
        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).

        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)
        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)
        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)
        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() 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
        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.Exception
        Shortcut for web application startup test (replacing TestSupport.execute(TestSupport.webApp(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)
        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.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)
        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.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)
        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.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)
        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.