Comprehensive testing framework and utilities for Dropwizard applications including JUnit 5 extensions, resource testing, and DAO helpers
—
Central application lifecycle management providing programmatic control over Dropwizard application startup, configuration, and shutdown during integration testing. These classes form the foundation for all Dropwizard testing utilities.
Main utility class for managing complete Dropwizard application lifecycle during testing, providing access to application components and network endpoints.
/**
* Core test support class for managing Dropwizard application lifecycle
* @param <C> Configuration type extending Configuration
*/
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
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);
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
@Nullable C configuration,
Function<Application<C>, Command> commandInstantiator);
// Lifecycle management
/**
* Initialize and start the Dropwizard application before tests
* @throws Exception if application startup fails
*/
public void before() throws Exception;
/**
* Stop and cleanup the Dropwizard application after tests
*/
public void after();
// Application access
/**
* Get the application configuration instance
* @return Configuration object used by the application
*/
public C getConfiguration();
/**
* Get the running Dropwizard application instance
* @return Application instance with full type information
*/
public <A extends Application<C>> A getApplication();
/**
* Virtual constructor for creating custom application instances
* Can be overridden for specialized application initialization
* @return New application instance
*/
public Application<C> newApplication();
/**
* Get the Dropwizard runtime environment
* @return Environment instance with access to metrics, validation, etc.
*/
public Environment getEnvironment();
/**
* Get the Jackson ObjectMapper used by the application
* @return ObjectMapper for JSON/YAML processing
*/
public ObjectMapper getObjectMapper();
// Network access
/**
* Get the main application HTTP port
* @return Port number for application endpoints
*/
public int getLocalPort();
/**
* Get the admin interface HTTP port
* @return Port number for admin endpoints
*/
public int getAdminPort();
/**
* Get the port for a specific connector by index
* @param connectorIndex Zero-based connector index
* @return Port number for the specified connector
*/
public int getPort(int connectorIndex);
// Configuration and extension
/**
* Add a lifecycle listener for application events
* @param listener ServiceListener instance for handling events
* @return This DropwizardTestSupport instance for chaining
*/
public DropwizardTestSupport<C> addListener(ServiceListener<C> listener);
/**
* Add a managed object to the application lifecycle
* @param managed Managed object to start/stop with application
* @return This DropwizardTestSupport instance for chaining
*/
public DropwizardTestSupport<C> manage(Managed managed);
}Usage Examples:
// Basic application testing with config file
DropwizardTestSupport<MyConfiguration> testSupport =
new DropwizardTestSupport<>(
MyApplication.class,
ResourceHelpers.resourceFilePath("test-config.yml")
);
testSupport.before(); // Start application
try {
// Test application
int port = testSupport.getLocalPort();
MyConfiguration config = testSupport.getConfiguration();
Environment env = testSupport.getEnvironment();
// Make HTTP requests, test components, etc.
} finally {
testSupport.after(); // Stop application
}
// With configuration overrides and random ports
DropwizardTestSupport<MyConfiguration> testSupport =
new DropwizardTestSupport<>(
MyApplication.class,
ResourceHelpers.resourceFilePath("test-config.yml"),
ConfigOverride.randomPorts(),
ConfigOverride.config("database.url", "jdbc:h2:mem:test")
);
// With pre-built configuration object
MyConfiguration config = new MyConfiguration();
config.setDatabaseUrl("jdbc:h2:mem:test");
DropwizardTestSupport<MyConfiguration> testSupport =
new DropwizardTestSupport<>(MyApplication.class, config);
// With lifecycle listener
testSupport.addListener(new ServiceListener<MyConfiguration>() {
@Override
public void onRun(MyConfiguration configuration,
Environment environment,
DropwizardTestSupport<MyConfiguration> rule) {
// Setup test data, mock services, etc.
}
@Override
public void onStop(DropwizardTestSupport<MyConfiguration> rule) {
// Cleanup test resources
}
});Abstract base class for handling application lifecycle events during testing, enabling custom setup and teardown logic.
/**
* Abstract listener for application lifecycle events
* @param <T> Configuration type extending Configuration
*/
public abstract static class ServiceListener<T extends Configuration> {
/**
* Called when the application has started and is ready for testing
* @param configuration Application configuration instance
* @param environment Dropwizard environment instance
* @param rule DropwizardTestSupport instance managing the application
* @throws Exception if setup fails
*/
public void onRun(T configuration, Environment environment,
DropwizardTestSupport<T> rule) throws Exception {
// Override for custom setup logic
}
/**
* Called when the application is stopping after tests complete
* @param rule DropwizardTestSupport instance managing the application
* @throws Exception if cleanup fails
*/
public void onStop(DropwizardTestSupport<T> rule) throws Exception {
// Override for custom cleanup logic
}
}Configuration factory that uses pre-constructed configuration objects instead of parsing files, useful for programmatic configuration during testing.
/**
* Configuration factory using pre-built configuration objects
* @param <C> Configuration type extending Configuration
*/
public class POJOConfigurationFactory<C extends Configuration>
extends YamlConfigurationFactory<C> {
/**
* Create factory with pre-built configuration
* @param cfg Configuration object to return for all build() calls
*/
public POJOConfigurationFactory(C cfg);
/**
* Returns the pre-configured object, ignoring provider and path
* @param provider Ignored configuration source provider
* @param path Ignored configuration path
* @return The pre-configured configuration object
*/
public C build(ConfigurationSourceProvider provider, String path);
/**
* Returns the pre-configured object, ignoring file parameter
* @param file Ignored configuration file
* @return The pre-configured configuration object
*/
public C build(File file);
/**
* Returns the pre-configured object
* @return The pre-configured configuration object
*/
public C build();
}Utility class providing helper methods for working with classpath resources in test environments.
/**
* Utility class for classpath resource operations
*/
public class ResourceHelpers {
/**
* Convert a classpath resource location to an absolute file path
* Useful for loading test configuration files from classpath
* @param resourceClassPathLocation Classpath location (e.g., "test-config.yml")
* @return Absolute file path to the resource
*/
public static String resourceFilePath(String resourceClassPathLocation);
}Usage Examples:
// Load test configuration from classpath
String configPath = ResourceHelpers.resourceFilePath("test-config.yml");
DropwizardTestSupport<MyConfiguration> testSupport =
new DropwizardTestSupport<>(MyApplication.class, configPath);
// Use with different test configurations
String devConfigPath = ResourceHelpers.resourceFilePath("configs/dev-test.yml");
String integrationConfigPath = ResourceHelpers.resourceFilePath("configs/integration-test.yml");Install with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-testing