0
# Core Test Support
1
2
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.
3
4
## Capabilities
5
6
### DropwizardTestSupport
7
8
Main utility class for managing complete Dropwizard application lifecycle during testing, providing access to application components and network endpoints.
9
10
```java { .api }
11
/**
12
* Core test support class for managing Dropwizard application lifecycle
13
* @param <C> Configuration type extending Configuration
14
*/
15
public class DropwizardTestSupport<C extends Configuration> {
16
17
// Primary constructors
18
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
19
@Nullable String configPath,
20
ConfigOverride... configOverrides);
21
22
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
23
C configuration);
24
25
// Extended constructors
26
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
27
@Nullable String configPath,
28
@Nullable ConfigurationSourceProvider configSourceProvider,
29
ConfigOverride... configOverrides);
30
31
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
32
@Nullable String configPath,
33
@Nullable String customPropertyPrefix,
34
ConfigOverride... configOverrides);
35
36
public DropwizardTestSupport(Class<? extends Application<C>> applicationClass,
37
@Nullable C configuration,
38
Function<Application<C>, Command> commandInstantiator);
39
40
// Lifecycle management
41
/**
42
* Initialize and start the Dropwizard application before tests
43
* @throws Exception if application startup fails
44
*/
45
public void before() throws Exception;
46
47
/**
48
* Stop and cleanup the Dropwizard application after tests
49
*/
50
public void after();
51
52
// Application access
53
/**
54
* Get the application configuration instance
55
* @return Configuration object used by the application
56
*/
57
public C getConfiguration();
58
59
/**
60
* Get the running Dropwizard application instance
61
* @return Application instance with full type information
62
*/
63
public <A extends Application<C>> A getApplication();
64
65
/**
66
* Virtual constructor for creating custom application instances
67
* Can be overridden for specialized application initialization
68
* @return New application instance
69
*/
70
public Application<C> newApplication();
71
72
/**
73
* Get the Dropwizard runtime environment
74
* @return Environment instance with access to metrics, validation, etc.
75
*/
76
public Environment getEnvironment();
77
78
/**
79
* Get the Jackson ObjectMapper used by the application
80
* @return ObjectMapper for JSON/YAML processing
81
*/
82
public ObjectMapper getObjectMapper();
83
84
// Network access
85
/**
86
* Get the main application HTTP port
87
* @return Port number for application endpoints
88
*/
89
public int getLocalPort();
90
91
/**
92
* Get the admin interface HTTP port
93
* @return Port number for admin endpoints
94
*/
95
public int getAdminPort();
96
97
/**
98
* Get the port for a specific connector by index
99
* @param connectorIndex Zero-based connector index
100
* @return Port number for the specified connector
101
*/
102
public int getPort(int connectorIndex);
103
104
// Configuration and extension
105
/**
106
* Add a lifecycle listener for application events
107
* @param listener ServiceListener instance for handling events
108
* @return This DropwizardTestSupport instance for chaining
109
*/
110
public DropwizardTestSupport<C> addListener(ServiceListener<C> listener);
111
112
/**
113
* Add a managed object to the application lifecycle
114
* @param managed Managed object to start/stop with application
115
* @return This DropwizardTestSupport instance for chaining
116
*/
117
public DropwizardTestSupport<C> manage(Managed managed);
118
}
119
```
120
121
**Usage Examples:**
122
123
```java
124
// Basic application testing with config file
125
DropwizardTestSupport<MyConfiguration> testSupport =
126
new DropwizardTestSupport<>(
127
MyApplication.class,
128
ResourceHelpers.resourceFilePath("test-config.yml")
129
);
130
131
testSupport.before(); // Start application
132
try {
133
// Test application
134
int port = testSupport.getLocalPort();
135
MyConfiguration config = testSupport.getConfiguration();
136
Environment env = testSupport.getEnvironment();
137
138
// Make HTTP requests, test components, etc.
139
} finally {
140
testSupport.after(); // Stop application
141
}
142
143
// With configuration overrides and random ports
144
DropwizardTestSupport<MyConfiguration> testSupport =
145
new DropwizardTestSupport<>(
146
MyApplication.class,
147
ResourceHelpers.resourceFilePath("test-config.yml"),
148
ConfigOverride.randomPorts(),
149
ConfigOverride.config("database.url", "jdbc:h2:mem:test")
150
);
151
152
// With pre-built configuration object
153
MyConfiguration config = new MyConfiguration();
154
config.setDatabaseUrl("jdbc:h2:mem:test");
155
156
DropwizardTestSupport<MyConfiguration> testSupport =
157
new DropwizardTestSupport<>(MyApplication.class, config);
158
159
// With lifecycle listener
160
testSupport.addListener(new ServiceListener<MyConfiguration>() {
161
@Override
162
public void onRun(MyConfiguration configuration,
163
Environment environment,
164
DropwizardTestSupport<MyConfiguration> rule) {
165
// Setup test data, mock services, etc.
166
}
167
168
@Override
169
public void onStop(DropwizardTestSupport<MyConfiguration> rule) {
170
// Cleanup test resources
171
}
172
});
173
```
174
175
### ServiceListener
176
177
Abstract base class for handling application lifecycle events during testing, enabling custom setup and teardown logic.
178
179
```java { .api }
180
/**
181
* Abstract listener for application lifecycle events
182
* @param <T> Configuration type extending Configuration
183
*/
184
public abstract static class ServiceListener<T extends Configuration> {
185
186
/**
187
* Called when the application has started and is ready for testing
188
* @param configuration Application configuration instance
189
* @param environment Dropwizard environment instance
190
* @param rule DropwizardTestSupport instance managing the application
191
* @throws Exception if setup fails
192
*/
193
public void onRun(T configuration, Environment environment,
194
DropwizardTestSupport<T> rule) throws Exception {
195
// Override for custom setup logic
196
}
197
198
/**
199
* Called when the application is stopping after tests complete
200
* @param rule DropwizardTestSupport instance managing the application
201
* @throws Exception if cleanup fails
202
*/
203
public void onStop(DropwizardTestSupport<T> rule) throws Exception {
204
// Override for custom cleanup logic
205
}
206
}
207
```
208
209
### POJOConfigurationFactory
210
211
Configuration factory that uses pre-constructed configuration objects instead of parsing files, useful for programmatic configuration during testing.
212
213
```java { .api }
214
/**
215
* Configuration factory using pre-built configuration objects
216
* @param <C> Configuration type extending Configuration
217
*/
218
public class POJOConfigurationFactory<C extends Configuration>
219
extends YamlConfigurationFactory<C> {
220
221
/**
222
* Create factory with pre-built configuration
223
* @param cfg Configuration object to return for all build() calls
224
*/
225
public POJOConfigurationFactory(C cfg);
226
227
/**
228
* Returns the pre-configured object, ignoring provider and path
229
* @param provider Ignored configuration source provider
230
* @param path Ignored configuration path
231
* @return The pre-configured configuration object
232
*/
233
public C build(ConfigurationSourceProvider provider, String path);
234
235
/**
236
* Returns the pre-configured object, ignoring file parameter
237
* @param file Ignored configuration file
238
* @return The pre-configured configuration object
239
*/
240
public C build(File file);
241
242
/**
243
* Returns the pre-configured object
244
* @return The pre-configured configuration object
245
*/
246
public C build();
247
}
248
```
249
250
### ResourceHelpers
251
252
Utility class providing helper methods for working with classpath resources in test environments.
253
254
```java { .api }
255
/**
256
* Utility class for classpath resource operations
257
*/
258
public class ResourceHelpers {
259
260
/**
261
* Convert a classpath resource location to an absolute file path
262
* Useful for loading test configuration files from classpath
263
* @param resourceClassPathLocation Classpath location (e.g., "test-config.yml")
264
* @return Absolute file path to the resource
265
*/
266
public static String resourceFilePath(String resourceClassPathLocation);
267
}
268
```
269
270
**Usage Examples:**
271
272
```java
273
// Load test configuration from classpath
274
String configPath = ResourceHelpers.resourceFilePath("test-config.yml");
275
DropwizardTestSupport<MyConfiguration> testSupport =
276
new DropwizardTestSupport<>(MyApplication.class, configPath);
277
278
// Use with different test configurations
279
String devConfigPath = ResourceHelpers.resourceFilePath("configs/dev-test.yml");
280
String integrationConfigPath = ResourceHelpers.resourceFilePath("configs/integration-test.yml");
281
```