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.00
# Dropwizard Testing
1
2
A 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.
3
4
## Package Information
5
6
- **Package Name**: dropwizard-testing
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**: Add to Maven dependencies:
10
11
```xml
12
<dependency>
13
<groupId>io.dropwizard</groupId>
14
<artifactId>dropwizard-testing</artifactId>
15
<version>4.0.14</version>
16
<scope>test</scope>
17
</dependency>
18
```
19
20
## Core Imports
21
22
```java
23
import io.dropwizard.testing.DropwizardTestSupport;
24
import io.dropwizard.testing.ConfigOverride;
25
import io.dropwizard.testing.ResourceHelpers;
26
import io.dropwizard.testing.junit5.DropwizardAppExtension;
27
import io.dropwizard.testing.junit5.ResourceExtension;
28
import io.dropwizard.testing.junit5.DAOTestExtension;
29
```
30
31
## Basic Usage
32
33
```java
34
// JUnit 5 full application testing
35
@RegisterExtension
36
public static final DropwizardAppExtension<MyConfiguration> APP =
37
new DropwizardAppExtension<>(
38
MyApplication.class,
39
ResourceHelpers.resourceFilePath("test-config.yml"),
40
ConfigOverride.randomPorts()
41
);
42
43
@Test
44
public void testApplication() {
45
String response = APP.client()
46
.target("http://localhost:" + APP.getLocalPort() + "/api/users")
47
.request()
48
.get(String.class);
49
50
assertThat(response).isNotNull();
51
}
52
53
// Resource testing without full application
54
@RegisterExtension
55
public static final ResourceExtension RESOURCE = ResourceExtension.builder()
56
.addResource(new UserResource())
57
.build();
58
59
@Test
60
public void testResource() {
61
Response response = RESOURCE.target("/users")
62
.request()
63
.get();
64
65
assertThat(response.getStatus()).isEqualTo(200);
66
}
67
```
68
69
## Architecture
70
71
Dropwizard Testing is built around several key architectural components:
72
73
- **Test Support Layer**: Core lifecycle management for Dropwizard applications during testing
74
- **JUnit 5 Extensions**: Modern extension model integration providing declarative test setup
75
- **Configuration System**: Runtime configuration override mechanisms for test-specific settings
76
- **Resource Testing**: Isolated JAX-RS resource testing without full application bootstrap
77
- **Database Testing**: Hibernate DAO testing with automated schema management
78
- **Client Integration**: HTTP client utilities for integration testing
79
80
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.
81
82
## Capabilities
83
84
### Core Test Support
85
86
Central application lifecycle management for integration testing, providing programmatic control over Dropwizard application startup, configuration, and shutdown during tests.
87
88
```java { .api }
89
public class DropwizardTestSupport<C extends Configuration> {
90
// Primary constructors
91
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
92
@Nullable String configPath,
93
ConfigOverride... configOverrides);
94
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
95
C configuration);
96
97
// Extended constructors with configuration source provider
98
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
99
@Nullable String configPath,
100
@Nullable ConfigurationSourceProvider configSourceProvider,
101
ConfigOverride... configOverrides);
102
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
103
@Nullable String configPath,
104
@Nullable String customPropertyPrefix,
105
ConfigOverride... configOverrides);
106
107
// Advanced constructors with command customization
108
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
109
@Nullable String configPath,
110
@Nullable ConfigurationSourceProvider configSourceProvider,
111
@Nullable String customPropertyPrefix,
112
ConfigOverride... configOverrides);
113
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
114
@Nullable C configuration,
115
Function<Application<C>, Command> commandInstantiator);
116
117
// Lifecycle management
118
public void before() throws Exception;
119
public void after();
120
121
// Listener and management support
122
public DropwizardTestSupport<C> addListener(ServiceListener<C> listener);
123
public DropwizardTestSupport<C> manage(Managed managed);
124
125
// Application access
126
public C getConfiguration();
127
public <A extends Application<C>> A getApplication();
128
public Environment getEnvironment();
129
public ObjectMapper getObjectMapper();
130
public Application<C> newApplication();
131
132
// Network access
133
public int getLocalPort();
134
public int getAdminPort();
135
public int getPort(int connectorIndex);
136
}
137
```
138
139
[Core Test Support](./core-test-support.md)
140
141
### Configuration Overrides
142
143
Runtime configuration modification system enabling test-specific settings including random port assignment and property overrides without modifying configuration files.
144
145
```java { .api }
146
public abstract class ConfigOverride {
147
// Static factory methods
148
public static ConfigOverride config(String key, String value);
149
public static ConfigOverride config(String propertyPrefix, String key, String value);
150
public static ConfigOverride config(String key, Supplier<String> value);
151
public static ConfigOverride config(String propertyPrefix, String key, Supplier<String> value);
152
public static ConfigOverride randomPorts();
153
public static ConfigOverride randomPorts(String propertyPrefix);
154
155
// Abstract lifecycle methods
156
public abstract void addToSystemProperties();
157
public abstract void removeFromSystemProperties();
158
159
// Constants
160
static final String DEFAULT_PREFIX = "dw.";
161
}
162
```
163
164
[Configuration Overrides](./configuration-overrides.md)
165
166
### Resource Testing
167
168
Lightweight JAX-RS resource testing utilities providing isolated testing environment without full Dropwizard application bootstrap, ideal for unit testing individual REST endpoints.
169
170
```java { .api }
171
public class Resource {
172
public static ResourceExtension.Builder builder();
173
174
public static class Builder<B extends Builder<B>> {
175
public B addResource(Object resource);
176
public B addResource(Supplier<Object> resourceSupplier);
177
public B addProvider(Class<?> klass);
178
public B addProvider(Object provider);
179
public B addProvider(Supplier<Object> providerSupplier);
180
public B setMapper(ObjectMapper mapper);
181
public B setValidator(Validator validator);
182
public B setMetricRegistry(MetricRegistry metricRegistry);
183
public B addProperty(String property, Object value);
184
public B setClientConfigurator(Consumer<ClientConfig> clientConfigurator);
185
public B setTestContainerFactory(TestContainerFactory factory);
186
public B bootstrapLogging(boolean value);
187
}
188
189
public WebTarget target(String path);
190
public Client client();
191
public ObjectMapper getObjectMapper();
192
public Validator getValidator();
193
public Consumer<ClientConfig> getClientConfigurator();
194
public JerseyTest getJerseyTest();
195
public void before() throws Throwable;
196
public void after() throws Throwable;
197
}
198
```
199
200
[Resource Testing](./resource-testing.md)
201
202
### DAO Testing
203
204
Database testing utilities with automated Hibernate SessionFactory management, transaction handling, and H2 in-memory database setup for testing data access layers.
205
206
```java { .api }
207
public abstract class DAOTest {
208
public static abstract class Builder<B extends Builder<B>> {
209
public B setUrl(String url);
210
public B setUsername(String username);
211
public B setPassword(String password);
212
public B addEntityClass(Class<?> entityClass);
213
public B setProperty(String key, String value);
214
public abstract DAOTest build();
215
}
216
217
public SessionFactory getSessionFactory();
218
public <T> T inTransaction(Callable<T> call);
219
public void inTransaction(Runnable action);
220
public void before();
221
public void after();
222
}
223
```
224
225
[DAO Testing](./dao-testing.md)
226
227
### JUnit 5 Extensions
228
229
Modern JUnit 5 integration providing declarative test setup through annotations and extensions, supporting full application testing, resource testing, DAO testing, and lightweight client testing.
230
231
```java { .api }
232
public class DropwizardAppExtension<C extends Configuration>
233
implements DropwizardExtension, BeforeAllCallback, AfterAllCallback {
234
235
// Application access
236
public C getConfiguration();
237
public Application<C> getApplication();
238
public Environment getEnvironment();
239
public ObjectMapper getObjectMapper();
240
241
// Network access
242
public int getLocalPort();
243
public int getAdminPort();
244
245
// HTTP testing
246
public Client client();
247
}
248
249
public class ResourceExtension implements DropwizardExtension {
250
public static Builder builder();
251
public WebTarget target(String path);
252
public Client client();
253
}
254
255
public class DAOTestExtension implements DropwizardExtension {
256
public static Builder newBuilder();
257
public SessionFactory getSessionFactory();
258
public <T> T inTransaction(Callable<T> call);
259
}
260
```
261
262
[JUnit 5 Extensions](./junit5-extensions.md)
263
264
## Types
265
266
```java { .api }
267
// Core configuration interface
268
public interface Configuration {
269
// Marker interface for Dropwizard configurations
270
}
271
272
// Environment access
273
public class Environment {
274
// Dropwizard runtime environment
275
public ObjectMapper getObjectMapper();
276
public Validator getValidator();
277
public MetricRegistry metrics();
278
}
279
280
// Service lifecycle listener
281
public abstract static class ServiceListener<T extends Configuration> {
282
public void onRun(T configuration, Environment environment,
283
DropwizardTestSupport<T> rule) throws Exception {}
284
public void onStop(DropwizardTestSupport<T> rule) throws Exception {}
285
}
286
287
// Managed object interface
288
public interface Managed {
289
public void start() throws Exception;
290
public void stop() throws Exception;
291
}
292
293
// Resource helpers for classpath resources
294
public class ResourceHelpers {
295
public static String resourceFilePath(String resourceClassPathLocation);
296
}
297
298
// Validation result types
299
public interface Validator {
300
public <T> Set<ConstraintViolation<T>> validate(T object, Class<?>... groups);
301
}
302
303
// Jackson ObjectMapper for JSON/YAML processing
304
public class ObjectMapper {
305
// Jackson JSON/YAML processing
306
}
307
308
// JAX-RS WebTarget for HTTP testing
309
public interface WebTarget {
310
public WebTarget path(String path);
311
public Invocation.Builder request();
312
public Invocation.Builder request(String... acceptedResponseTypes);
313
}
314
315
// JAX-RS Client for HTTP operations
316
public interface Client {
317
public WebTarget target(String uri);
318
public void close();
319
}
320
321
// Hibernate SessionFactory for database access
322
public interface SessionFactory {
323
public Session openSession();
324
public void close();
325
}
326
327
// Response wrapper for HTTP responses
328
public abstract class Response {
329
public abstract int getStatus();
330
public abstract <T> T readEntity(Class<T> entityType);
331
public abstract void close();
332
}
333
334
// Jersey test container factory for resource testing
335
public interface TestContainerFactory {
336
// Factory for creating test containers
337
}
338
339
// Metrics registry for application metrics
340
public class MetricRegistry {
341
// Dropwizard metrics registry
342
}
343
344
// Jersey test framework integration
345
public abstract class JerseyTest {
346
// Jersey test framework for resource testing
347
}
348
```