Class TestSupportBuilder<C extends io.dropwizard.Configuration>

  • Type Parameters:
    C - configuration type

    public class TestSupportBuilder<C extends io.dropwizard.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 Detail

      • TestSupportBuilder

        public TestSupportBuilder​(java.lang.Class<? extends io.dropwizard.Application<C>> app)
    • Method Detail

      • 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.
        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
      • listen

        public TestSupportBuilder<C> listen​(TestSupportBuilder.TestListener<C> listener)
        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)

        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)

        Returns:
        dropwizard test support implementation
      • runCore

        public RunResult<C> runCore()
                             throws java.lang.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:
        run(webApp(appClass, configPath, overrides)
        Throws:
        java.lang.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 java.lang.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:
        java.lang.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 java.lang.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:
        test support object used for execution (for example, to examine configuration)
        Throws:
        java.lang.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 java.lang.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:
        java.lang.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)