@Retention(value=RUNTIME) @Target(value=TYPE) @ExtendWith(value=TestDropwizardAppExtension.class) @Inherited public @interface TestDropwizardApp
Extension may be declared on test class or on any nested class. When declared on nested class, applies to all lower nested classes (default junit 5 extension appliance behaviour). Multiple extension declarations (on multiple nested levels) is useless as junit will use only the top declared extension instance (and other will be ignored).
Application started before all tests (before BeforeAll) and shut down
after all tests (after AfterAll). There is no way to restart it between tests. Group
test, not modifying internal application state and extract test modifying state to separate classes (possibly
junit nested tests).
Guice injections will work on test fields annotated with Inject or
Inject (Injector.injectMembers(Object) applied on test instance).
Guice AOP will not work on test methods (because test itself is not created by guice).
Test constructor, lifecycle and test methods may use additional parameters:
Jit to force JIT
binding (create declared bean with guice, even if it wasn't registered)Application or exact application classObjectMapperClientSupport for calling application web
endpoints (or external urls). Also it provides actual application ports.
Internally use DropwizardTestSupport.
It is possible to apply extension manually using RegisterExtension
and TestDropwizardAppExtension.forApp(Class) builder. The only difference is declaration type, but in both
cases extension will work the same way.
| Modifier and Type | Required Element and Description |
|---|---|
java.lang.Class<? extends io.dropwizard.Application<?>> |
value |
| Modifier and Type | Optional Element and Description |
|---|---|
java.lang.String |
config |
java.lang.String[] |
configOverride
Each value must be written as
key: value. |
java.lang.Class<? extends GuiceyConfigurationHook>[] |
hooks
Hooks provide access to guice builder allowing complete customization of application context
in tests.
|
boolean |
randomPorts
Enables random ports usage.
|
java.lang.String |
restMapping
Specifies rest mapping path.
|
public abstract java.lang.Class<? extends io.dropwizard.Application<?>> value
public abstract java.lang.String config
public abstract java.lang.String[] configOverride
key: value.public abstract java.lang.Class<? extends GuiceyConfigurationHook>[] hooks
For anonymous hooks you can simply declare hook as static field:
@EnableHook static GuiceyConfigurationHook hook = builder -> builder.disableExtension(Something.class)
All such fields will be detected automatically and hooks registered. Hooks declared in base test classes
are also counted.
for more info,
EnableHookpublic abstract boolean randomPorts
To get port numbers in test use ClientSupport parameter
in lifecycle or test method:
static beforeAll(ClientSupport client) {
String baseUrl = "http://localhost:" + client.getPort();
String baseAdminUrl = "http://localhost:" + client.getAdminPort();
}
Or use client target methods directly.public abstract java.lang.String restMapping
configOverride()
"server.rootMapping=/something/*". Specified value would be prefixed with "/" and, if required
"/*" applied at the end. So it would be correct to specify restMapping = "api" (actually set value
would be "/api/*").
This option is only intended to simplify cases when custom configuration file is not yet used in tests (usually early PoC phase). It allows you to map servlet into application root in test (because rest is no more resides in root). When used with existing configuration file, this parameter will override file definition.