Class GuiceyTestSupport<C extends io.dropwizard.core.Configuration>

  • Type Parameters:
    C - configuration type

    public class GuiceyTestSupport<C extends io.dropwizard.core.Configuration>
    extends io.dropwizard.testing.DropwizardTestSupport<C>
    An alternative to DropwizardTestSupport which does not run jetty (web part) allowing to test only guice context. Internally, TestCommand used instead of ServerCommand.

    Supposed to be used in cases when application startup fail must be tested: new GuiceyTestSupport(MyApp.class, (String) null).before().

    Since:
    03.02.2022
    • Nested Class Summary

      • Nested classes/interfaces inherited from class io.dropwizard.testing.DropwizardTestSupport

        io.dropwizard.testing.DropwizardTestSupport.ServiceListener<T extends io.dropwizard.core.Configuration>
    • Field Summary

      • Fields inherited from class io.dropwizard.testing.DropwizardTestSupport

        application, applicationClass, commandInstantiator, configOverrides, configPath, configSourceProvider, configuration, customPropertyPrefix, environment, explicitConfig, jettyServer, listeners
    • Constructor Summary

      Constructors 
      Constructor Description
      GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration)
      Create test support.
      GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, java.lang.String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, io.dropwizard.testing.ConfigOverride... configOverrides)
      Create test support.
      GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, java.lang.String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, java.lang.String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)
      Create test support.
      GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, java.lang.String configPath, io.dropwizard.testing.ConfigOverride... configOverrides)
      Create test support.
      GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass, java.lang.String configPath, java.lang.String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)
      Create test support.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void after()  
      GuiceyTestSupport<C> configModifiers​(java.util.List<ConfigModifier<C>> modifiers)
      Register configuration modifiers.
      GuiceyTestSupport<C> configModifiers​(ConfigModifier<C>... modifiers)
      Register configuration modifiers.
      GuiceyTestSupport<C> disableManagedLifecycle()
      By default, guicey simulates jetty lifecycle to support for Managed and LifeCycle objects.
      <T> T getBean​(com.google.inject.Key<T> key)
      Shortcut for accessing guice beans.
      <T> T getBean​(java.lang.Class<T> type)
      Shortcut for accessing guice beans.
      RunResult<C> run()
      Normally, DropwizardTestSupport.before() and after() methods are called separately.
      <T> T run​(TestSupport.RunCallback<T> callback)
      Normally, DropwizardTestSupport.before() and after() methods are called separately.
      • Methods inherited from class io.dropwizard.testing.DropwizardTestSupport

        addListener, before, getAdminPort, getApplication, getConfiguration, getEnvironment, getLocalPort, getObjectMapper, getPort, manage, newApplication
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GuiceyTestSupport

        public GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable
                                 java.lang.String configPath,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Create test support.
        Parameters:
        applicationClass - application class
        configPath - configuration file path
        configOverrides - configuration overrides
      • GuiceyTestSupport

        public GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable
                                 java.lang.String configPath,
                                 @Nullable
                                 io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Create test support.
        Parameters:
        applicationClass - application class
        configPath - configuration file path
        configSourceProvider - configuration source provider (optional)
        configOverrides - configuration overrides
      • GuiceyTestSupport

        public GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable
                                 java.lang.String configPath,
                                 @Nullable
                                 io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider,
                                 @Nullable
                                 java.lang.String customPropertyPrefix,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Create test support.
        Parameters:
        applicationClass - application class
        configPath - configuration file path
        configSourceProvider - configuration source provider
        customPropertyPrefix - configuration overrides prefix
        configOverrides - configuration overrides
      • GuiceyTestSupport

        public GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable
                                 java.lang.String configPath,
                                 @Nullable
                                 java.lang.String customPropertyPrefix,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Create test support.
        Parameters:
        applicationClass - application class
        configPath - configuration file path
        customPropertyPrefix - configuration overrides prefix
        configOverrides - configuration overrides
      • GuiceyTestSupport

        public GuiceyTestSupport​(java.lang.Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 C configuration)
        Create test support.
        Parameters:
        applicationClass - application class
        configuration - manual configuration instance
    • Method Detail

      • disableManagedLifecycle

        public GuiceyTestSupport<C> disableManagedLifecycle()
        By default, guicey simulates jetty lifecycle to support for Managed and LifeCycle objects.

        It might be required in test to avoid starting managed objects (especially all managed in application) because important (for test) services replaced with mocks (and no need to wait for the rest of the application).

        Returns:
        test support instance for chained calls
      • configModifiers

        @SafeVarargs
        public final GuiceyTestSupport<C> configModifiers​(ConfigModifier<C>... modifiers)
        Register configuration modifiers.
        Parameters:
        modifiers - configuration modifiers
        Returns:
        support object instance for chained calls
      • configModifiers

        public GuiceyTestSupport<C> configModifiers​(java.util.List<ConfigModifier<C>> modifiers)
        Register configuration modifiers.
        Parameters:
        modifiers - configuration modifiers
        Returns:
        support object instance for chained calls
      • run

        public <T> T run​(@Nullable
                         TestSupport.RunCallback<T> callback)
                  throws java.lang.Exception
        Normally, DropwizardTestSupport.before() and after() methods are called separately. This method is a shortcut mostly for errors testing when DropwizardTestSupport.before() assumed to fail to make sure after() will be called in any case: testSupport.run(null).
        Type Parameters:
        T - result type
        Parameters:
        callback - callback (may be null)
        Returns:
        callback result
        Throws:
        java.lang.Exception - any appeared exception
      • run

        public RunResult<C> run()
                         throws java.lang.Exception
        Normally, DropwizardTestSupport.before() and after() methods are called separately. This method is a shortcut mostly for errors testing when DropwizardTestSupport.before() assumed to fail to make sure after() will be called in any case: testSupport.run(null).
        Returns:
        execution result (with all required objects for verification)
        Throws:
        java.lang.Exception - any appeared exception
      • getBean

        public <T> T getBean​(java.lang.Class<T> type)
        Shortcut for accessing guice beans.
        Type Parameters:
        T - bean type
        Parameters:
        type - target bean type
        Returns:
        bean instance
      • getBean

        public <T> T getBean​(com.google.inject.Key<T> key)
        Shortcut for accessing guice beans.
        Type Parameters:
        T - bean type
        Parameters:
        key - binding key
        Returns:
        bean instance
      • after

        public void after()
        Overrides:
        after in class io.dropwizard.testing.DropwizardTestSupport<C extends io.dropwizard.core.Configuration>