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

java.lang.Object
ru.vyarus.dropwizard.guice.test.builder.BaseBuilder<C,TestSupportBuilder<C>>
ru.vyarus.dropwizard.guice.test.builder.TestSupportBuilder<C>
Type Parameters:
C - configuration type

public class TestSupportBuilder<C extends io.dropwizard.core.Configuration> extends BaseBuilder<C,TestSupportBuilder<C>>
Builder and runner for DropwizardTestSupport and GuiceyTestSupport objects. Allows using all available options. This builder should be suitable for cases when junit 5 extensions could not be used.

Use TestSupport.build(Class) to build instance.

Builder is not supposed to be used for multiple runs: registered hooks will be applied only once. This limitation is not possible to avoid because builder could be used for support objects creation, which are not aware of hooks. So hooks could be registered globally only in time of addition to the builder.

Since:
14.11.2023
  • Constructor Details

    • TestSupportBuilder

      public TestSupportBuilder(Class<? extends io.dropwizard.core.Application<C>> app)
      Create builder.
      Parameters:
      app - application type
  • Method Details

    • randomPorts

      public TestSupportBuilder<C> randomPorts()
      Shortcut to enable random web ports.
      Returns:
      builder instance for chained calls
    • randomPorts

      public TestSupportBuilder<C> randomPorts(boolean randomPorts)
      Use random http ports (applicable only for web). Useful to separate concurrent web instances runs.
      Parameters:
      randomPorts - true to use random ports
      Returns:
      builder instance for chained calls
    • clientFactory

      public TestSupportBuilder<C> clientFactory(TestClientFactory factory)
      Custom client factory implementation used for ClientSupport object creation (this special client class automatically constructs base urls for application under test, based on its configuration).

      Client instance could be accessed at any time (during test) with TestSupportHolder.getClient()

      Parameters:
      factory - factory instance
      Returns:
      builder instance for chained calls
    • apacheClient

      public TestSupportBuilder<C> apacheClient()
      Shortcut for clientFactory(ru.vyarus.dropwizard.guice.test.client.TestClientFactory) to configure ApacheTestClientFactory. The default HttpUrlConnectorProvider supports only HTTP 1.1 methods and have problem with PATCH method usage on jdk > 16.
      Returns:
      builder instance for chained calls
    • listen

      Listener used ONLY when builder run methods used! Listener may be used to perform additional initialization or cleanup before/after application execution.
      Parameters:
      listener - execution listener
      Returns:
      builder instance for chained calls
    • buildCore

      public GuiceyTestSupport<C> buildCore()
      Build a test support object with web services DropwizardTestSupport. Method supposed to be used only by TestSupport for support objects creation. Prefer direct run (runCore()) method usage (used support object could be easily obtained with TestSupport.getContext() in any place).

      IMPORTANT: listeners could not be used (because they are implemented as a custom run callback). Custom TestClientFactory would also be lost! Use direct run methods to not lose them.

      Returns:
      guicey test support implementation
    • buildWeb

      public io.dropwizard.testing.DropwizardTestSupport<C> buildWeb()
      Build a test support object with web services DropwizardTestSupport. Method supposed to be used only by TestSupport for support objects creation. Prefer direct run (runWeb()) method usage (used support object could be easily obtained with TestSupport.getContext() in any place).

      IMPORTANT: listeners could not be used (because they are implemented as a custom run callback). Custom TestClientFactory would also be lost! Use direct run methods to not lose them.

      Returns:
      dropwizard test support implementation
    • runCore

      public RunResult<C> runCore() throws Exception
      Start and stop application without web services. Mostly useful to test application startup errors (with proper application shutdown).

      NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.

      Returns:
      action 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)
    • runCore

      public <T> T runCore(@Nullable TestSupport.RunCallback<T> action) throws Exception
      Start and stop application without web services. Provided action would be executed in time of application life.

      NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.

      Type Parameters:
      T - result type
      Parameters:
      action - action to execute while the application is running
      Returns:
      action 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)
    • runCoreWithoutManaged

      public RunResult<C> runCoreWithoutManaged() throws Exception
      Start and stop application without web services. Provided action would be executed in time of application life. Does not simulate Managed objects lifecycle (start/stop would not be called).

      NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.

      Returns:
      execution result (with all required objects for verification)
      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)
    • runCoreWithoutManaged

      public <T> T runCoreWithoutManaged(@Nullable TestSupport.RunCallback<T> action) throws Exception
      Start and stop application without web services. Provided action would be executed in time of application life. Does not simulate Managed objects lifecycle (start/stop would not be called).

      NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.

      Type Parameters:
      T - result type
      Parameters:
      action - action to execute while the application is running
      Returns:
      action 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)
    • runWeb

      public RunResult<C> runWeb() throws Exception
      Start and stop application with web services. Mostly useful to test application startup errors (with proper application shutdown).

      NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.

      Returns:
      result action
      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)
    • runWeb

      public <T> T runWeb(@Nullable TestSupport.RunCallback<T> action) throws Exception
      Start and stop application with web services. Provided action would be executed in time of application life.

      NOTE: method not supposed to be used for multiple calls. For example, registered hooks would only work on first execution.

      Type Parameters:
      T - result type
      Parameters:
      action - action to execute while the application is running
      Returns:
      action 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)