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

java.lang.Object
io.dropwizard.testing.DropwizardTestSupport<C>
ru.vyarus.dropwizard.guice.test.GuiceyTestSupport<C>
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(Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration)
    Create test support.
    GuiceyTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, io.dropwizard.testing.ConfigOverride... configOverrides)
    Create test support.
    GuiceyTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)
    Create test support.
    GuiceyTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, String configPath, io.dropwizard.testing.ConfigOverride... configOverrides)
    Create test support.
    GuiceyTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, String configPath, String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)
    Create test support.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    Register configuration modifiers.
    Register configuration modifiers.
    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(Class<T> type)
    Shortcut for accessing guice beans.
    run()
    Normally, DropwizardTestSupport.before() and after() methods are called separately.
    <T> T
    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 Details

    • GuiceyTestSupport

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

      public GuiceyTestSupport(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable 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(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, @Nullable io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, @Nullable 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(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, @Nullable 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(Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration)
      Create test support.
      Parameters:
      applicationClass - application class
      configuration - manual configuration instance
  • Method Details

    • 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(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 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:
      Exception - any appeared exception
    • run

      public RunResult<C> run() throws 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:
      Exception - any appeared exception
    • getBean

      public <T> T getBean(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>