Comprehensive testing framework and utilities for Dropwizard applications including JUnit 5 extensions, resource testing, and DAO helpers
npx @tessl/cli install tessl/maven-io-dropwizard--dropwizard-testing@4.0.0A comprehensive testing framework and utilities for Dropwizard applications, providing essential tools for unit and integration testing including JUnit 5 extensions, resource testing utilities, DAO helpers, and configuration override mechanisms.
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<version>4.0.14</version>
<scope>test</scope>
</dependency>import io.dropwizard.testing.DropwizardTestSupport;
import io.dropwizard.testing.ConfigOverride;
import io.dropwizard.testing.ResourceHelpers;
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import io.dropwizard.testing.junit5.ResourceExtension;
import io.dropwizard.testing.junit5.DAOTestExtension;// JUnit 5 full application testing
@RegisterExtension
public static final DropwizardAppExtension<MyConfiguration> APP =
new DropwizardAppExtension<>(
MyApplication.class,
ResourceHelpers.resourceFilePath("test-config.yml"),
ConfigOverride.randomPorts()
);
@Test
public void testApplication() {
String response = APP.client()
.target("http://localhost:" + APP.getLocalPort() + "/api/users")
.request()
.get(String.class);
assertThat(response).isNotNull();
}
// Resource testing without full application
@RegisterExtension
public static final ResourceExtension RESOURCE = ResourceExtension.builder()
.addResource(new UserResource())
.build();
@Test
public void testResource() {
Response response = RESOURCE.target("/users")
.request()
.get();
assertThat(response.getStatus()).isEqualTo(200);
}Dropwizard Testing is built around several key architectural components:
The library follows a layered approach where JUnit 5 extensions provide the modern testing interface, while core support classes handle the underlying Dropwizard application lifecycle management.
Central application lifecycle management for integration testing, providing programmatic control over Dropwizard application startup, configuration, and shutdown during tests.
public class DropwizardTestSupport<C extends Configuration> {
// Primary constructors
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
@Nullable String configPath,
ConfigOverride... configOverrides);
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
C configuration);
// Extended constructors with configuration source provider
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
@Nullable String configPath,
@Nullable ConfigurationSourceProvider configSourceProvider,
ConfigOverride... configOverrides);
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
@Nullable String configPath,
@Nullable String customPropertyPrefix,
ConfigOverride... configOverrides);
// Advanced constructors with command customization
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
@Nullable String configPath,
@Nullable ConfigurationSourceProvider configSourceProvider,
@Nullable String customPropertyPrefix,
ConfigOverride... configOverrides);
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
@Nullable C configuration,
Function<Application<C>, Command> commandInstantiator);
// Lifecycle management
public void before() throws Exception;
public void after();
// Listener and management support
public DropwizardTestSupport<C> addListener(ServiceListener<C> listener);
public DropwizardTestSupport<C> manage(Managed managed);
// Application access
public C getConfiguration();
public <A extends Application<C>> A getApplication();
public Environment getEnvironment();
public ObjectMapper getObjectMapper();
public Application<C> newApplication();
// Network access
public int getLocalPort();
public int getAdminPort();
public int getPort(int connectorIndex);
}Runtime configuration modification system enabling test-specific settings including random port assignment and property overrides without modifying configuration files.
public abstract class ConfigOverride {
// Static factory methods
public static ConfigOverride config(String key, String value);
public static ConfigOverride config(String propertyPrefix, String key, String value);
public static ConfigOverride config(String key, Supplier<String> value);
public static ConfigOverride config(String propertyPrefix, String key, Supplier<String> value);
public static ConfigOverride randomPorts();
public static ConfigOverride randomPorts(String propertyPrefix);
// Abstract lifecycle methods
public abstract void addToSystemProperties();
public abstract void removeFromSystemProperties();
// Constants
static final String DEFAULT_PREFIX = "dw.";
}Lightweight JAX-RS resource testing utilities providing isolated testing environment without full Dropwizard application bootstrap, ideal for unit testing individual REST endpoints.
public class Resource {
public static ResourceExtension.Builder builder();
public static class Builder<B extends Builder<B>> {
public B addResource(Object resource);
public B addResource(Supplier<Object> resourceSupplier);
public B addProvider(Class<?> klass);
public B addProvider(Object provider);
public B addProvider(Supplier<Object> providerSupplier);
public B setMapper(ObjectMapper mapper);
public B setValidator(Validator validator);
public B setMetricRegistry(MetricRegistry metricRegistry);
public B addProperty(String property, Object value);
public B setClientConfigurator(Consumer<ClientConfig> clientConfigurator);
public B setTestContainerFactory(TestContainerFactory factory);
public B bootstrapLogging(boolean value);
}
public WebTarget target(String path);
public Client client();
public ObjectMapper getObjectMapper();
public Validator getValidator();
public Consumer<ClientConfig> getClientConfigurator();
public JerseyTest getJerseyTest();
public void before() throws Throwable;
public void after() throws Throwable;
}Database testing utilities with automated Hibernate SessionFactory management, transaction handling, and H2 in-memory database setup for testing data access layers.
public abstract class DAOTest {
public static abstract class Builder<B extends Builder<B>> {
public B setUrl(String url);
public B setUsername(String username);
public B setPassword(String password);
public B addEntityClass(Class<?> entityClass);
public B setProperty(String key, String value);
public abstract DAOTest build();
}
public SessionFactory getSessionFactory();
public <T> T inTransaction(Callable<T> call);
public void inTransaction(Runnable action);
public void before();
public void after();
}Modern JUnit 5 integration providing declarative test setup through annotations and extensions, supporting full application testing, resource testing, DAO testing, and lightweight client testing.
public class DropwizardAppExtension<C extends Configuration>
implements DropwizardExtension, BeforeAllCallback, AfterAllCallback {
// Application access
public C getConfiguration();
public Application<C> getApplication();
public Environment getEnvironment();
public ObjectMapper getObjectMapper();
// Network access
public int getLocalPort();
public int getAdminPort();
// HTTP testing
public Client client();
}
public class ResourceExtension implements DropwizardExtension {
public static Builder builder();
public WebTarget target(String path);
public Client client();
}
public class DAOTestExtension implements DropwizardExtension {
public static Builder newBuilder();
public SessionFactory getSessionFactory();
public <T> T inTransaction(Callable<T> call);
}// Core configuration interface
public interface Configuration {
// Marker interface for Dropwizard configurations
}
// Environment access
public class Environment {
// Dropwizard runtime environment
public ObjectMapper getObjectMapper();
public Validator getValidator();
public MetricRegistry metrics();
}
// Service lifecycle listener
public abstract static class ServiceListener<T extends Configuration> {
public void onRun(T configuration, Environment environment,
DropwizardTestSupport<T> rule) throws Exception {}
public void onStop(DropwizardTestSupport<T> rule) throws Exception {}
}
// Managed object interface
public interface Managed {
public void start() throws Exception;
public void stop() throws Exception;
}
// Resource helpers for classpath resources
public class ResourceHelpers {
public static String resourceFilePath(String resourceClassPathLocation);
}
// Validation result types
public interface Validator {
public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups);
}
// Jackson ObjectMapper for JSON/YAML processing
public class ObjectMapper {
// Jackson JSON/YAML processing
}
// JAX-RS WebTarget for HTTP testing
public interface WebTarget {
public WebTarget path(String path);
public Invocation.Builder request();
public Invocation.Builder request(String... acceptedResponseTypes);
}
// JAX-RS Client for HTTP operations
public interface Client {
public WebTarget target(String uri);
public void close();
}
// Hibernate SessionFactory for database access
public interface SessionFactory {
public Session openSession();
public void close();
}
// Response wrapper for HTTP responses
public abstract class Response {
public abstract int getStatus();
public abstract <T> T readEntity(Class<T> entityType);
public abstract void close();
}
// Jersey test container factory for resource testing
public interface TestContainerFactory {
// Factory for creating test containers
}
// Metrics registry for application metrics
public class MetricRegistry {
// Dropwizard metrics registry
}
// Jersey test framework integration
public abstract class JerseyTest {
// Jersey test framework for resource testing
}