Annotation Type TestDropwizardApp


  • @Retention(RUNTIME)
    @Target(TYPE)
    @ExtendWith(TestDropwizardAppExtension.class)
    @Inherited
    public @interface TestDropwizardApp
    Dropwizard app junit 5 extension. Runs complete dropwizard application. Useful for testing web endpoints.

    Extension may be declared on test class or on any nested class. When declared on nested class, applies to all lower nested classes (default junit 5 extension appliance behaviour). Multiple extension declarations (on multiple nested levels) is useless as junit will use only the top declared extension instance (and other will be ignored).

    Application started before all tests (before BeforeAll) and shut down after all tests (after AfterAll). There is no way to restart it between tests. Group test, not modifying internal application state and extract test modifying state to separate classes (possibly junit nested tests).

    Guice injections will work on test fields annotated with Inject or Inject (Injector.injectMembers(Object) applied on test instance). Guice AOP will not work on test methods (because test itself is not created by guice).

    Test constructor, lifecycle and test methods may use additional parameters:

    • Any declared (possibly with qualifier annotation or generified) guice bean
    • For not declared beans use Jit to force JIT binding (create declared bean with guice, even if it wasn't registered)
    • Specially supported objects:
      • Application or exact application class
      • ObjectMapper
      • ClientSupport for calling application web endpoints (or external urls). Also it provides actual application ports.

    Internally use DropwizardTestSupport.

    It is possible to apply extension manually using RegisterExtension and TestDropwizardAppExtension.forApp(Class) builder. The only difference is declaration type, but in both cases extension will work the same way.

    Since:
    28.04.2020
    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      java.lang.Class<? extends io.dropwizard.Application<?>> value  
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      java.lang.String config  
      java.lang.String[] configOverride
      Each value must be written as key: value.
      java.lang.Class<? extends GuiceyConfigurationHook>[] hooks
      Hooks provide access to guice builder allowing complete customization of application context in tests.
      boolean randomPorts
      Enables random ports usage.
      java.lang.String restMapping
      Specifies rest mapping path.
    • Element Detail

      • value

        java.lang.Class<? extends io.dropwizard.Application<?>> value
        Returns:
        application class
      • config

        java.lang.String config
        Returns:
        path to configuration file (optional)
        Default:
        ""
      • configOverride

        java.lang.String[] configOverride
        Each value must be written as key: value.

        In order to specify raw ConfigOverride values (for delayed evaluation) use direct extension registration with RegisterExtension instead of annotation.

        Returns:
        list of overridden configuration values (may be used even without real configuration)
        Default:
        {}
      • hooks

        java.lang.Class<? extends GuiceyConfigurationHook>[] hooks
        Hooks provide access to guice builder allowing complete customization of application context in tests.

        For anonymous hooks you can simply declare hook as static field: @EnableHook static GuiceyConfigurationHook hook = builder -> builder.disableExtension(Something.class) All such fields will be detected automatically and hooks registered. Hooks declared in base test classes are also counted.

        Returns:
        list of hooks to use
        See Also:
        for more info, EnableHook
        Default:
        {}
      • randomPorts

        boolean randomPorts
        Enables random ports usage. Supports both simple and default dropwizard servers. Random ports would be set even if you specify exact configuration file with configured ports (option overrides configuration).

        To get port numbers in test use ClientSupport parameter in lifecycle or test method:

        
         static beforeAll(ClientSupport client) {
             String baseUrl = "http://localhost:" + client.getPort();
             String baseAdminUrl = "http://localhost:" + client.getAdminPort();
         }
         
        Or use client target methods directly.
        Returns:
        true to use random ports
        Default:
        false
      • restMapping

        java.lang.String restMapping
        Specifies rest mapping path. This is the same as specifying configOverride() "server.rootMapping=/something/*". Specified value would be prefixed with "/" and, if required "/*" applied at the end. So it would be correct to specify restMapping = "api" (actually set value would be "/api/*").

        This option is only intended to simplify cases when custom configuration file is not yet used in tests (usually early PoC phase). It allows you to map servlet into application root in test (because rest is no more resides in root). When used with existing configuration file, this parameter will override file definition.

        Returns:
        rest mapping (empty string - do nothing)
        Default:
        ""