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

  • Type Parameters:
    C - the configuration type
    All Implemented Interfaces:
    org.junit.rules.TestRule

    public class DropwizardAppRule<C extends io.dropwizard.core.Configuration>
    extends org.junit.rules.ExternalResource
    A JUnit rule for starting and stopping your application at the start and end of a test class.

    By default, the Application will be constructed using reflection to invoke the nullary constructor. If your application does not provide a public nullary constructor, you will need to override the newApplication() method to provide your application instance(s).

    Using DropwizardAppRule at the suite level can speed up test runs, as the application is only started and stopped once for the entire suite:

     @RunWith(Suite.class)
     @SuiteClasses({FooTest.class, BarTest.class})
     public class MySuite {
       @ClassRule
       public static final DropwizardAppRule<MyConfig> DROPWIZARD = new DropwizardAppRule<>(...);
     }
     

    If the same instance of DropwizardAppRule is reused at the suite- and class-level, then the application will be started and stopped once, regardless of whether the entire suite or a single test is executed.

     public class FooTest {
       @ClassRule public static final DropwizardAppRule<MyConfig> DROPWIZARD = MySuite.DROPWIZARD;
    
       public void testFoo() { ... }
     }
    
     public class BarTest {
       @ClassRule public static final DropwizardAppRule<MyConfig> DROPWIZARD = MySuite.DROPWIZARD;
    
       public void testBar() { ... }
     }
     

    • Constructor Summary

      Constructors 
      Constructor Description
      DropwizardAppRule​(io.dropwizard.testing.DropwizardTestSupport<C> testSupport)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, @Nullable String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, @Nullable String customPropertyPrefix, Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, @Nullable String customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, @Nullable String customPropertyPrefix, Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, Optional<String> customPropertyPrefix, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, @Nullable String configPath, Optional<String> customPropertyPrefix, Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator, io.dropwizard.testing.ConfigOverride... configOverrides)  
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration)
      Alternate constructor that allows specifying exact Configuration object to use, instead of reading a resource and binding it as Configuration object.
      DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass, C configuration, Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator)
      Alternate constructor that allows specifying the command the Dropwizard application is started with.
    • Constructor Detail

      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass)
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Since:
        2.0
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 Optional<String> customPropertyPrefix,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 @Nullable String customPropertyPrefix,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Since:
        2.0
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider,
                                 @Nullable String customPropertyPrefix,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Since:
        2.0
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 Optional<String> customPropertyPrefix,
                                 Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 @Nullable String customPropertyPrefix,
                                 Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Since:
        2.0
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 @Nullable String configPath,
                                 io.dropwizard.configuration.ConfigurationSourceProvider configSourceProvider,
                                 @Nullable String customPropertyPrefix,
                                 Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator,
                                 io.dropwizard.testing.ConfigOverride... configOverrides)
        Since:
        2.0
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 C configuration)
        Alternate constructor that allows specifying exact Configuration object to use, instead of reading a resource and binding it as Configuration object.
        Since:
        0.9
      • DropwizardAppRule

        public DropwizardAppRule​(Class<? extends io.dropwizard.core.Application<C>> applicationClass,
                                 C configuration,
                                 Function<io.dropwizard.core.Application<C>,​io.dropwizard.core.cli.Command> commandInstantiator)
        Alternate constructor that allows specifying the command the Dropwizard application is started with.
        Since:
        1.1.0
      • DropwizardAppRule

        public DropwizardAppRule​(io.dropwizard.testing.DropwizardTestSupport<C> testSupport)
    • Method Detail

      • manage

        public DropwizardAppRule<C> manage​(io.dropwizard.lifecycle.Managed managed)
      • before

        protected void before()
                       throws Exception
        Overrides:
        before in class org.junit.rules.ExternalResource
        Throws:
        Exception
      • after

        protected void after()
        Overrides:
        after in class org.junit.rules.ExternalResource
      • getConfiguration

        public C getConfiguration()
      • getLocalPort

        public int getLocalPort()
      • getPort

        public int getPort​(int connectorIndex)
      • getAdminPort

        public int getAdminPort()
      • newApplication

        public io.dropwizard.core.Application<C> newApplication()
      • getApplication

        public <A extends io.dropwizard.core.Application<C>> A getApplication()
      • getEnvironment

        public io.dropwizard.core.setup.Environment getEnvironment()
      • getObjectMapper

        public com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()
      • getTestSupport

        public io.dropwizard.testing.DropwizardTestSupport<C> getTestSupport()
      • client

        public jakarta.ws.rs.client.Client client()
        Returns a new HTTP Jersey Client for performing HTTP requests against the tested Dropwizard server. The client can be reused across different tests and automatically closed along with the server. The client can be augmented by overriding the clientBuilder() method.
        Returns:
        a new Client managed by the rule.
      • clientBuilder

        protected org.glassfish.jersey.client.JerseyClientBuilder clientBuilder()