Package io.dropwizard.testing.junit
Class DropwizardAppRule<C extends io.dropwizard.core.Configuration>
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- io.dropwizard.testing.junit.DropwizardAppRule<C>
-
- 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.ExternalResourceA JUnit rule for starting and stopping your application at the start and end of a test class.By default, the
Applicationwill be constructed using reflection to invoke the nullary constructor. If your application does not provide a public nullary constructor, you will need to override thenewApplication()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() { ... } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDropwizardAppRule.ServiceListener<T extends io.dropwizard.core.Configuration>
-
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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DropwizardAppRule<C>addListener(DropwizardAppRule.ServiceListener<C> listener)protected voidafter()protected voidbefore()jakarta.ws.rs.client.Clientclient()Returns a new HTTP JerseyClientfor performing HTTP requests against the tested Dropwizard server.protected org.glassfish.jersey.client.JerseyClientBuilderclientBuilder()intgetAdminPort()<A extends io.dropwizard.core.Application<C>>
AgetApplication()CgetConfiguration()io.dropwizard.core.setup.EnvironmentgetEnvironment()intgetLocalPort()com.fasterxml.jackson.databind.ObjectMappergetObjectMapper()intgetPort(int connectorIndex)io.dropwizard.testing.DropwizardTestSupport<C>getTestSupport()DropwizardAppRule<C>manage(io.dropwizard.lifecycle.Managed managed)io.dropwizard.core.Application<C>newApplication()
-
-
-
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
-
addListener
public DropwizardAppRule<C> addListener(DropwizardAppRule.ServiceListener<C> listener)
-
manage
public DropwizardAppRule<C> manage(io.dropwizard.lifecycle.Managed managed)
-
before
protected void before() throws Exception- Overrides:
beforein classorg.junit.rules.ExternalResource- Throws:
Exception
-
after
protected void after()
- Overrides:
afterin classorg.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 JerseyClientfor 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 theclientBuilder()method.- Returns:
- a new
Clientmanaged by the rule.
-
clientBuilder
protected org.glassfish.jersey.client.JerseyClientBuilder clientBuilder()
-
-