0
# Configuration Overrides
1
2
Runtime configuration modification system enabling test-specific settings including random port assignment and property overrides without modifying configuration files. Essential for creating isolated test environments.
3
4
## Capabilities
5
6
### ConfigOverride
7
8
Abstract base class providing configuration override functionality for modifying Dropwizard application settings at runtime during testing.
9
10
```java { .api }
11
/**
12
* Abstract base class for runtime configuration overrides
13
*/
14
public abstract class ConfigOverride {
15
16
// Static factory methods for simple key-value overrides
17
/**
18
* Create a simple configuration override with default prefix
19
* @param key Configuration property key (will be prefixed with "dw.")
20
* @param value Configuration property value
21
* @return ConfigOverride instance for the key-value pair
22
*/
23
public static ConfigOverride config(String key, String value);
24
25
/**
26
* Create a configuration override with custom prefix
27
* @param propertyPrefix Custom prefix for the configuration property
28
* @param key Configuration property key
29
* @param value Configuration property value
30
* @return ConfigOverride instance for the prefixed key-value pair
31
*/
32
public static ConfigOverride config(String propertyPrefix, String key, String value);
33
34
// Static factory methods for lazy-evaluated overrides
35
/**
36
* Create a configuration override with lazy-evaluated value
37
* @param key Configuration property key (will be prefixed with "dw.")
38
* @param value Supplier providing the configuration value when needed
39
* @return ConfigOverride instance with lazy evaluation
40
*/
41
public static ConfigOverride config(String key, Supplier<String> value);
42
43
/**
44
* Create a configuration override with custom prefix and lazy evaluation
45
* @param propertyPrefix Custom prefix for the configuration property
46
* @param key Configuration property key
47
* @param value Supplier providing the configuration value when needed
48
* @return ConfigOverride instance with custom prefix and lazy evaluation
49
*/
50
public static ConfigOverride config(String propertyPrefix, String key, Supplier<String> value);
51
52
// Static factory methods for random port assignment
53
/**
54
* Create random port assignments for server.applicationConnectors[0].port and
55
* server.adminConnectors[0].port using default prefix
56
* @return ConfigOverride instance for random port assignment
57
*/
58
public static ConfigOverride randomPorts();
59
60
/**
61
* Create random port assignments with custom property prefix
62
* @param propertyPrefix Custom prefix for port configuration properties
63
* @return ConfigOverride instance for random port assignment with custom prefix
64
*/
65
public static ConfigOverride randomPorts(String propertyPrefix);
66
67
// Abstract lifecycle methods
68
/**
69
* Apply the configuration override to system properties
70
* Called before application startup
71
*/
72
public abstract void addToSystemProperties();
73
74
/**
75
* Remove the configuration override from system properties
76
* Called after application shutdown for cleanup
77
*/
78
public abstract void removeFromSystemProperties();
79
80
// Constants
81
/**
82
* Default property prefix used by Dropwizard configuration system
83
*/
84
public static final String DEFAULT_PREFIX = "dw.";
85
}
86
```
87
88
**Usage Examples:**
89
90
```java
91
// Simple property override
92
ConfigOverride dbOverride = ConfigOverride.config("database.url", "jdbc:h2:mem:test");
93
94
// Custom prefix override
95
ConfigOverride customOverride = ConfigOverride.config("myapp.", "feature.enabled", "true");
96
97
// Lazy evaluation for dynamic values
98
ConfigOverride dynamicOverride = ConfigOverride.config("timestamp",
99
() -> String.valueOf(System.currentTimeMillis()));
100
101
// Random port assignment (most common use case)
102
ConfigOverride randomPorts = ConfigOverride.randomPorts();
103
104
// Random ports with custom prefix
105
ConfigOverride customRandomPorts = ConfigOverride.randomPorts("myapp.");
106
107
// Use with DropwizardTestSupport
108
DropwizardTestSupport<MyConfiguration> testSupport =
109
new DropwizardTestSupport<>(
110
MyApplication.class,
111
"test-config.yml",
112
ConfigOverride.randomPorts(),
113
ConfigOverride.config("database.url", "jdbc:h2:mem:test"),
114
ConfigOverride.config("redis.enabled", "false")
115
);
116
117
// Multiple overrides for complex test scenarios
118
ConfigOverride[] overrides = {
119
ConfigOverride.randomPorts(),
120
ConfigOverride.config("database.url", "jdbc:h2:mem:test"),
121
ConfigOverride.config("database.user", "sa"),
122
ConfigOverride.config("database.password", ""),
123
ConfigOverride.config("logging.level", "DEBUG"),
124
ConfigOverride.config("metrics.enabled", "false")
125
};
126
127
DropwizardTestSupport<MyConfiguration> testSupport =
128
new DropwizardTestSupport<>(MyApplication.class, "test-config.yml", overrides);
129
```
130
131
### ConfigOverrideValue
132
133
Concrete implementation of ConfigOverride for specific key-value configuration overrides. This class is package-private but is created by the ConfigOverride.config() factory methods.
134
135
```java { .api }
136
/**
137
* Concrete implementation for key-value configuration overrides
138
* Created by ConfigOverride.config() factory methods
139
*/
140
class ConfigOverrideValue extends ConfigOverride {
141
// Package-private implementation class
142
// Use ConfigOverride.config() factory methods to create instances
143
}
144
```
145
146
### ConfigOverrideRandomPorts
147
148
Concrete implementation of ConfigOverride for automatic random port assignment. This class is package-private but is created by the ConfigOverride.randomPorts() factory methods.
149
150
```java { .api }
151
/**
152
* Concrete implementation for random port assignment
153
* Created by ConfigOverride.randomPorts() factory methods
154
*/
155
class ConfigOverrideRandomPorts extends ConfigOverride {
156
// Package-private implementation class
157
// Use ConfigOverride.randomPorts() factory methods to create instances
158
}
159
```
160
161
## Common Configuration Override Patterns
162
163
### Database Configuration
164
165
```java
166
// H2 in-memory database for testing
167
ConfigOverride.config("database.url", "jdbc:h2:mem:test"),
168
ConfigOverride.config("database.user", "sa"),
169
ConfigOverride.config("database.password", ""),
170
ConfigOverride.config("database.driverClass", "org.h2.Driver")
171
172
// PostgreSQL test database
173
ConfigOverride.config("database.url", "jdbc:postgresql://localhost:5432/test_db"),
174
ConfigOverride.config("database.user", "test_user"),
175
ConfigOverride.config("database.password", "test_password")
176
```
177
178
### Logging Configuration
179
180
```java
181
// Enable debug logging for tests
182
ConfigOverride.config("logging.level", "DEBUG"),
183
ConfigOverride.config("logging.loggers.com.mycompany", "TRACE")
184
185
// Disable logging for performance tests
186
ConfigOverride.config("logging.level", "OFF")
187
```
188
189
### Feature Toggles
190
191
```java
192
// Disable external service integrations
193
ConfigOverride.config("externalServices.enabled", "false"),
194
ConfigOverride.config("caching.enabled", "false"),
195
ConfigOverride.config("metrics.enabled", "false")
196
197
// Enable test-specific features
198
ConfigOverride.config("testMode.enabled", "true"),
199
ConfigOverride.config("testData.autoLoad", "true")
200
```
201
202
### Network Configuration
203
204
```java
205
// Random ports (most common)
206
ConfigOverride.randomPorts()
207
208
// Specific ports for integration tests
209
ConfigOverride.config("server.applicationConnectors[0].port", "8080"),
210
ConfigOverride.config("server.adminConnectors[0].port", "8081")
211
212
// Custom connector configurations
213
ConfigOverride.config("server.applicationConnectors[0].type", "http"),
214
ConfigOverride.config("server.applicationConnectors[0].bindHost", "localhost")
215
```
216
217
### Security Configuration
218
219
```java
220
// Disable authentication for testing
221
ConfigOverride.config("auth.enabled", "false"),
222
ConfigOverride.config("csrf.enabled", "false")
223
224
// Test-specific authentication
225
ConfigOverride.config("auth.testMode", "true"),
226
ConfigOverride.config("auth.testUser", "test@example.com")
227
```